home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / cse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-27  |  242.9 KB  |  8,138 lines  |  [TEXT/MPS ]

  1. /* Common subexpression elimination for GNU compiler.
  2.    Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "rtl.h"
  23. #include "regs.h"
  24. #include "hard-reg-set.h"
  25. #include "flags.h"
  26. #include "real.h"
  27. #include "insn-config.h"
  28. #include "recog.h"
  29.  
  30. #include <stdio.h>
  31. #include <setjmp.h>
  32.  
  33. /* The basic idea of common subexpression elimination is to go
  34.    through the code, keeping a record of expressions that would
  35.    have the same value at the current scan point, and replacing
  36.    expressions encountered with the cheapest equivalent expression.
  37.  
  38.    It is too complicated to keep track of the different possibilities
  39.    when control paths merge; so, at each label, we forget all that is
  40.    known and start fresh.  This can be described as processing each
  41.    basic block separately.  Note, however, that these are not quite
  42.    the same as the basic blocks found by a later pass and used for
  43.    data flow analysis and register packing.  We do not need to start fresh
  44.    after a conditional jump instruction if there is no label there.
  45.  
  46.    We use two data structures to record the equivalent expressions:
  47.    a hash table for most expressions, and several vectors together
  48.    with "quantity numbers" to record equivalent (pseudo) registers.
  49.  
  50.    The use of the special data structure for registers is desirable
  51.    because it is faster.  It is possible because registers references
  52.    contain a fairly small number, the register number, taken from
  53.    a contiguously allocated series, and two register references are
  54.    identical if they have the same number.  General expressions
  55.    do not have any such thing, so the only way to retrieve the
  56.    information recorded on an expression other than a register
  57.    is to keep it in a hash table.
  58.  
  59. Registers and "quantity numbers":
  60.    
  61.    At the start of each basic block, all of the (hardware and pseudo)
  62.    registers used in the function are given distinct quantity
  63.    numbers to indicate their contents.  During scan, when the code
  64.    copies one register into another, we copy the quantity number.
  65.    When a register is loaded in any other way, we allocate a new
  66.    quantity number to describe the value generated by this operation.
  67.    `reg_qty' records what quantity a register is currently thought
  68.    of as containing.
  69.  
  70.    All real quantity numbers are greater than or equal to `max_reg'.
  71.    If register N has not been assigned a quantity, reg_qty[N] will equal N.
  72.  
  73.    Quantity numbers below `max_reg' do not exist and none of the `qty_...'
  74.    variables should be referenced with an index below `max_reg'.
  75.  
  76.    We also maintain a bidirectional chain of registers for each
  77.    quantity number.  `qty_first_reg', `qty_last_reg',
  78.    `reg_next_eqv' and `reg_prev_eqv' hold these chains.
  79.  
  80.    The first register in a chain is the one whose lifespan is least local.
  81.    Among equals, it is the one that was seen first.
  82.    We replace any equivalent register with that one.
  83.  
  84.    If two registers have the same quantity number, it must be true that
  85.    REG expressions with `qty_mode' must be in the hash table for both
  86.    registers and must be in the same class.
  87.  
  88.    The converse is not true.  Since hard registers may be referenced in
  89.    any mode, two REG expressions might be equivalent in the hash table
  90.    but not have the same quantity number if the quantity number of one
  91.    of the registers is not the same mode as those expressions.
  92.    
  93. Constants and quantity numbers
  94.  
  95.    When a quantity has a known constant value, that value is stored
  96.    in the appropriate element of qty_const.  This is in addition to
  97.    putting the constant in the hash table as is usual for non-regs.
  98.  
  99.    Whether a reg or a constant is preferred is determined by the configuration
  100.    macro CONST_COSTS and will often depend on the constant value.  In any
  101.    event, expressions containing constants can be simplified, by fold_rtx.
  102.  
  103.    When a quantity has a known nearly constant value (such as an address
  104.    of a stack slot), that value is stored in the appropriate element
  105.    of qty_const.
  106.  
  107.    Integer constants don't have a machine mode.  However, cse
  108.    determines the intended machine mode from the destination
  109.    of the instruction that moves the constant.  The machine mode
  110.    is recorded in the hash table along with the actual RTL
  111.    constant expression so that different modes are kept separate.
  112.  
  113. Other expressions:
  114.  
  115.    To record known equivalences among expressions in general
  116.    we use a hash table called `table'.  It has a fixed number of buckets
  117.    that contain chains of `struct table_elt' elements for expressions.
  118.    These chains connect the elements whose expressions have the same
  119.    hash codes.
  120.  
  121.    Other chains through the same elements connect the elements which
  122.    currently have equivalent values.
  123.  
  124.    Register references in an expression are canonicalized before hashing
  125.    the expression.  This is done using `reg_qty' and `qty_first_reg'.
  126.    The hash code of a register reference is computed using the quantity
  127.    number, not the register number.
  128.  
  129.    When the value of an expression changes, it is necessary to remove from the
  130.    hash table not just that expression but all expressions whose values
  131.    could be different as a result.
  132.  
  133.      1. If the value changing is in memory, except in special cases
  134.      ANYTHING referring to memory could be changed.  That is because
  135.      nobody knows where a pointer does not point.
  136.      The function `invalidate_memory' removes what is necessary.
  137.  
  138.      The special cases are when the address is constant or is
  139.      a constant plus a fixed register such as the frame pointer
  140.      or a static chain pointer.  When such addresses are stored in,
  141.      we can tell exactly which other such addresses must be invalidated
  142.      due to overlap.  `invalidate' does this.
  143.      All expressions that refer to non-constant
  144.      memory addresses are also invalidated.  `invalidate_memory' does this.
  145.  
  146.      2. If the value changing is a register, all expressions
  147.      containing references to that register, and only those,
  148.      must be removed.
  149.  
  150.    Because searching the entire hash table for expressions that contain
  151.    a register is very slow, we try to figure out when it isn't necessary.
  152.    Precisely, this is necessary only when expressions have been
  153.    entered in the hash table using this register, and then the value has
  154.    changed, and then another expression wants to be added to refer to
  155.    the register's new value.  This sequence of circumstances is rare
  156.    within any one basic block.
  157.  
  158.    The vectors `reg_tick' and `reg_in_table' are used to detect this case.
  159.    reg_tick[i] is incremented whenever a value is stored in register i.
  160.    reg_in_table[i] holds -1 if no references to register i have been
  161.    entered in the table; otherwise, it contains the value reg_tick[i] had
  162.    when the references were entered.  If we want to enter a reference
  163.    and reg_in_table[i] != reg_tick[i], we must scan and remove old references.
  164.    Until we want to enter a new entry, the mere fact that the two vectors
  165.    don't match makes the entries be ignored if anyone tries to match them.
  166.  
  167.    Registers themselves are entered in the hash table as well as in
  168.    the equivalent-register chains.  However, the vectors `reg_tick'
  169.    and `reg_in_table' do not apply to expressions which are simple
  170.    register references.  These expressions are removed from the table
  171.    immediately when they become invalid, and this can be done even if
  172.    we do not immediately search for all the expressions that refer to
  173.    the register.
  174.  
  175.    A CLOBBER rtx in an instruction invalidates its operand for further
  176.    reuse.  A CLOBBER or SET rtx whose operand is a MEM:BLK
  177.    invalidates everything that resides in memory.
  178.  
  179. Related expressions:
  180.  
  181.    Constant expressions that differ only by an additive integer
  182.    are called related.  When a constant expression is put in
  183.    the table, the related expression with no constant term
  184.    is also entered.  These are made to point at each other
  185.    so that it is possible to find out if there exists any
  186.    register equivalent to an expression related to a given expression.  */
  187.    
  188. /* One plus largest register number used in this function.  */
  189.  
  190. static int max_reg;
  191.  
  192. /* Length of vectors indexed by quantity number.
  193.    We know in advance we will not need a quantity number this big.  */
  194.  
  195. static int max_qty;
  196.  
  197. /* Next quantity number to be allocated.
  198.    This is 1 + the largest number needed so far.  */
  199.  
  200. static int next_qty;
  201.  
  202. /* Indexed by quantity number, gives the first (or last) (pseudo) register 
  203.    in the chain of registers that currently contain this quantity.  */
  204.  
  205. static int *qty_first_reg;
  206. static int *qty_last_reg;
  207.  
  208. /* Index by quantity number, gives the mode of the quantity.  */
  209.  
  210. static enum machine_mode *qty_mode;
  211.  
  212. /* Indexed by quantity number, gives the rtx of the constant value of the
  213.    quantity, or zero if it does not have a known value.
  214.    A sum of the frame pointer (or arg pointer) plus a constant
  215.    can also be entered here.  */
  216.  
  217. static rtx *qty_const;
  218.  
  219. /* Indexed by qty number, gives the insn that stored the constant value
  220.    recorded in `qty_const'.  */
  221.  
  222. static rtx *qty_const_insn;
  223.  
  224. /* The next three variables are used to track when a comparison between a
  225.    quantity and some constant or register has been passed.  In that case, we
  226.    know the results of the comparison in case we see it again.  These variables
  227.    record a comparison that is known to be true.  */
  228.  
  229. /* Indexed by qty number, gives the rtx code of a comparison with a known
  230.    result involving this quantity.  If none, it is UNKNOWN.  */
  231. static enum rtx_code *qty_comparison_code;
  232.  
  233. /* Indexed by qty number, gives the constant being compared against in a
  234.    comparison of known result.  If no such comparison, it is undefined.
  235.    If the comparison is not with a constant, it is zero.  */
  236.  
  237. static rtx *qty_comparison_const;
  238.  
  239. /* Indexed by qty number, gives the quantity being compared against in a
  240.    comparison of known result.  If no such comparison, if it undefined.
  241.    If the comparison is not with a register, it is -1.  */
  242.  
  243. static int *qty_comparison_qty;
  244.  
  245. #ifdef HAVE_cc0
  246. /* For machines that have a CC0, we do not record its value in the hash
  247.    table since its use is guaranteed to be the insn immediately following
  248.    its definition and any other insn is presumed to invalidate it.
  249.  
  250.    Instead, we store below the value last assigned to CC0.  If it should
  251.    happen to be a constant, it is stored in preference to the actual
  252.    assigned value.  In case it is a constant, we store the mode in which
  253.    the constant should be interpreted.  */
  254.  
  255. static rtx prev_insn_cc0;
  256. static enum machine_mode prev_insn_cc0_mode;
  257. #endif
  258.  
  259. /* Previous actual insn.  0 if at first insn of basic block.  */
  260.  
  261. static rtx prev_insn;
  262.  
  263. /* Insn being scanned.  */
  264.  
  265. static rtx this_insn;
  266.  
  267. /* Index by (pseudo) register number, gives the quantity number
  268.    of the register's current contents.  */
  269.  
  270. static int *reg_qty;
  271.  
  272. /* Index by (pseudo) register number, gives the number of the next (or
  273.    previous) (pseudo) register in the chain of registers sharing the same
  274.    value.
  275.  
  276.    Or -1 if this register is at the end of the chain.
  277.  
  278.    If reg_qty[N] == N, reg_next_eqv[N] is undefined.  */
  279.  
  280. static int *reg_next_eqv;
  281. static int *reg_prev_eqv;
  282.  
  283. /* Index by (pseudo) register number, gives the number of times
  284.    that register has been altered in the current basic block.  */
  285.  
  286. static int *reg_tick;
  287.  
  288. /* Index by (pseudo) register number, gives the reg_tick value at which
  289.    rtx's containing this register are valid in the hash table.
  290.    If this does not equal the current reg_tick value, such expressions
  291.    existing in the hash table are invalid.
  292.    If this is -1, no expressions containing this register have been
  293.    entered in the table.  */
  294.  
  295. static int *reg_in_table;
  296.  
  297. /* A HARD_REG_SET containing all the hard registers for which there is 
  298.    currently a REG expression in the hash table.  Note the difference
  299.    from the above variables, which indicate if the REG is mentioned in some
  300.    expression in the table.  */
  301.  
  302. static HARD_REG_SET hard_regs_in_table;
  303.  
  304. /* A HARD_REG_SET containing all the hard registers that are invalidated
  305.    by a CALL_INSN.  */
  306.  
  307. static HARD_REG_SET regs_invalidated_by_call;
  308.  
  309. /* Two vectors of ints:
  310.    one containing max_reg -1's; the other max_reg + 500 (an approximation
  311.    for max_qty) elements where element i contains i.
  312.    These are used to initialize various other vectors fast.  */
  313.  
  314. static int *all_minus_one;
  315. static int *consec_ints;
  316.  
  317. /* CUID of insn that starts the basic block currently being cse-processed.  */
  318.  
  319. static int cse_basic_block_start;
  320.  
  321. /* CUID of insn that ends the basic block currently being cse-processed.  */
  322.  
  323. static int cse_basic_block_end;
  324.  
  325. /* Vector mapping INSN_UIDs to cuids.
  326.    The cuids are like uids but increase monotonically always.
  327.    We use them to see whether a reg is used outside a given basic block.  */
  328.  
  329. static int *uid_cuid;
  330.  
  331. /* Highest UID in UID_CUID.  */
  332. static int max_uid;
  333.  
  334. /* Get the cuid of an insn.  */
  335.  
  336. #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
  337.  
  338. /* Nonzero if cse has altered conditional jump insns
  339.    in such a way that jump optimization should be redone.  */
  340.  
  341. static int cse_jumps_altered;
  342.  
  343. /* canon_hash stores 1 in do_not_record
  344.    if it notices a reference to CC0, PC, or some other volatile
  345.    subexpression.  */
  346.  
  347. static int do_not_record;
  348.  
  349. /* canon_hash stores 1 in hash_arg_in_memory
  350.    if it notices a reference to memory within the expression being hashed.  */
  351.  
  352. static int hash_arg_in_memory;
  353.  
  354. /* canon_hash stores 1 in hash_arg_in_struct
  355.    if it notices a reference to memory that's part of a structure.  */
  356.  
  357. static int hash_arg_in_struct;
  358.  
  359. /* The hash table contains buckets which are chains of `struct table_elt's,
  360.    each recording one expression's information.
  361.    That expression is in the `exp' field.
  362.  
  363.    Those elements with the same hash code are chained in both directions
  364.    through the `next_same_hash' and `prev_same_hash' fields.
  365.  
  366.    Each set of expressions with equivalent values
  367.    are on a two-way chain through the `next_same_value'
  368.    and `prev_same_value' fields, and all point with
  369.    the `first_same_value' field at the first element in
  370.    that chain.  The chain is in order of increasing cost.
  371.    Each element's cost value is in its `cost' field.
  372.  
  373.    The `in_memory' field is nonzero for elements that
  374.    involve any reference to memory.  These elements are removed
  375.    whenever a write is done to an unidentified location in memory.
  376.    To be safe, we assume that a memory address is unidentified unless
  377.    the address is either a symbol constant or a constant plus
  378.    the frame pointer or argument pointer.
  379.  
  380.    The `in_struct' field is nonzero for elements that
  381.    involve any reference to memory inside a structure or array.
  382.  
  383.    The `related_value' field is used to connect related expressions
  384.    (that differ by adding an integer).
  385.    The related expressions are chained in a circular fashion.
  386.    `related_value' is zero for expressions for which this
  387.    chain is not useful.
  388.  
  389.    The `cost' field stores the cost of this element's expression.
  390.  
  391.    The `is_const' flag is set if the element is a constant (including
  392.    a fixed address).
  393.  
  394.    The `flag' field is used as a temporary during some search routines.
  395.  
  396.    The `mode' field is usually the same as GET_MODE (`exp'), but
  397.    if `exp' is a CONST_INT and has no machine mode then the `mode'
  398.    field is the mode it was being used as.  Each constant is
  399.    recorded separately for each mode it is used with.  */
  400.  
  401.  
  402. struct table_elt
  403. {
  404.   rtx exp;
  405.   struct table_elt *next_same_hash;
  406.   struct table_elt *prev_same_hash;
  407.   struct table_elt *next_same_value;
  408.   struct table_elt *prev_same_value;
  409.   struct table_elt *first_same_value;
  410.   struct table_elt *related_value;
  411.   int cost;
  412.   enum machine_mode mode;
  413.   char in_memory;
  414.   char in_struct;
  415.   char is_const;
  416.   char flag;
  417. };
  418.  
  419. #define HASHBITS 16
  420.  
  421. /* We don't want a lot of buckets, because we rarely have very many
  422.    things stored in the hash table, and a lot of buckets slows
  423.    down a lot of loops that happen frequently.  */
  424. #define NBUCKETS 31
  425.  
  426. /* Compute hash code of X in mode M.  Special-case case where X is a pseudo
  427.    register (hard registers may require `do_not_record' to be set).  */
  428.  
  429. #define HASH(X, M)    \
  430.  (GET_CODE (X) == REG && REGNO (X) >= FIRST_PSEUDO_REGISTER    \
  431.   ? ((((int) REG << 7) + reg_qty[REGNO (X)]) % NBUCKETS)    \
  432.   : canon_hash (X, M) % NBUCKETS)
  433.  
  434. /* Determine whether register number N is considered a fixed register for CSE.
  435.    It is desirable to replace other regs with fixed regs, to reduce need for
  436.    non-fixed hard regs.
  437.    A reg wins if it is either the frame pointer or designated as fixed,
  438.    but not if it is an overlapping register.  */
  439. #ifdef OVERLAPPING_REGNO_P
  440. #define FIXED_REGNO_P(N)  \
  441.   (((N) == FRAME_POINTER_REGNUM || fixed_regs[N])    \
  442.    && ! OVERLAPPING_REGNO_P ((N)))
  443. #else
  444. #define FIXED_REGNO_P(N)  \
  445.   ((N) == FRAME_POINTER_REGNUM || fixed_regs[N])
  446. #endif
  447.  
  448. /* Compute cost of X, as stored in the `cost' field of a table_elt.  Fixed
  449.    hard registers are the cheapest with a cost of 0.  Next come pseudos
  450.    with a cost of one and other hard registers with a cost of 2.  Aside
  451.    from these special cases, call `rtx_cost'.  */
  452.  
  453. #define COST(X)                        \
  454.   (GET_CODE (X) == REG                    \
  455.    ? (REGNO (X) >= FIRST_PSEUDO_REGISTER ? 1        \
  456.       : (FIXED_REGNO_P (REGNO (X))            \
  457.      && REGNO_REG_CLASS (REGNO (X)) != NO_REGS) ? 0    \
  458.       : 2)                        \
  459.    : rtx_cost (X, SET) * 2)
  460.  
  461. /* Determine if the quantity number for register X represents a valid index
  462.    into the `qty_...' variables.  */
  463.  
  464. #define REGNO_QTY_VALID_P(N) (reg_qty[N] != (N))
  465.  
  466. static struct table_elt *table[NBUCKETS];
  467.  
  468. /* Chain of `struct table_elt's made so far for this function
  469.    but currently removed from the table.  */
  470.  
  471. static struct table_elt *free_element_chain;
  472.  
  473. /* Number of `struct table_elt' structures made so far for this function.  */
  474.  
  475. static int n_elements_made;
  476.  
  477. /* Maximum value `n_elements_made' has had so far in this compilation
  478.    for functions previously processed.  */
  479.  
  480. static int max_elements_made;
  481.  
  482. /* Surviving equivalence class when two equivalence classes are merged 
  483.    by recording the effects of a jump in the last insn.  Zero if the
  484.    last insn was not a conditional jump.  */
  485.  
  486. static struct table_elt *last_jump_equiv_class;
  487.  
  488. /* Set to the cost of a constant pool reference if one was found for a
  489.    symbolic constant.  If this was found, it means we should try to
  490.    convert constants into constant pool entries if they don't fit in
  491.    the insn.  */
  492.  
  493. static int constant_pool_entries_cost;
  494.  
  495. /* Bits describing what kind of values in memory must be invalidated
  496.    for a particular instruction.  If all three bits are zero,
  497.    no memory refs need to be invalidated.  Each bit is more powerful
  498.    than the preceding ones, and if a bit is set then the preceding
  499.    bits are also set.
  500.  
  501.    Here is how the bits are set:
  502.    Pushing onto the stack invalidates only the stack pointer,
  503.    writing at a fixed address invalidates only variable addresses,
  504.    writing in a structure element at variable address
  505.      invalidates all but scalar variables,
  506.    and writing in anything else at variable address invalidates everything.  */
  507.  
  508. struct write_data
  509. {
  510.   int sp : 1;            /* Invalidate stack pointer. */
  511.   int var : 1;            /* Invalidate variable addresses.  */
  512.   int nonscalar : 1;        /* Invalidate all but scalar variables.  */
  513.   int all : 1;            /* Invalidate all memory refs.  */
  514. };
  515.  
  516. /* Nonzero if X has the form (PLUS frame-pointer integer).  We check for
  517.    virtual regs here because the simplify_*_operation routines are called
  518.    by integrate.c, which is called before virtual register instantiation.  */
  519.  
  520. #define FIXED_BASE_PLUS_P(X)                    \
  521.   ((X) == frame_pointer_rtx || (X) == arg_pointer_rtx        \
  522.    || (X) == virtual_stack_vars_rtx                \
  523.    || (X) == virtual_incoming_args_rtx                \
  524.    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
  525.        && (XEXP (X, 0) == frame_pointer_rtx            \
  526.        || XEXP (X, 0) == arg_pointer_rtx            \
  527.        || XEXP (X, 0) == virtual_stack_vars_rtx        \
  528.        || XEXP (X, 0) == virtual_incoming_args_rtx)))
  529.  
  530. /* Similar, but also allows reference to the stack pointer.
  531.  
  532.    This used to include FIXED_BASE_PLUS_P, however, we can't assume that
  533.    arg_pointer_rtx by itself is nonzero, because on at least one machine,
  534.    the i960, the arg pointer is zero when it is unused.  */
  535.  
  536. #define NONZERO_BASE_PLUS_P(X)                    \
  537.   ((X) == frame_pointer_rtx                    \
  538.    || (X) == virtual_stack_vars_rtx                \
  539.    || (X) == virtual_incoming_args_rtx                \
  540.    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
  541.        && (XEXP (X, 0) == frame_pointer_rtx            \
  542.        || XEXP (X, 0) == arg_pointer_rtx            \
  543.        || XEXP (X, 0) == virtual_stack_vars_rtx        \
  544.        || XEXP (X, 0) == virtual_incoming_args_rtx))    \
  545.    || (X) == stack_pointer_rtx                    \
  546.    || (X) == virtual_stack_dynamic_rtx                \
  547.    || (X) == virtual_outgoing_args_rtx                \
  548.    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
  549.        && (XEXP (X, 0) == stack_pointer_rtx            \
  550.        || XEXP (X, 0) == virtual_stack_dynamic_rtx        \
  551.        || XEXP (X, 0) == virtual_outgoing_args_rtx)))
  552.  
  553. static struct table_elt *lookup ();
  554. static void free_element ();
  555.  
  556. static int insert_regs ();
  557. static void rehash_using_reg ();
  558. static void remove_invalid_refs ();
  559. static int exp_equiv_p ();
  560. int refers_to_p ();
  561. int refers_to_mem_p ();
  562. static void invalidate_from_clobbers ();
  563. static int safe_hash ();
  564. static int canon_hash ();
  565. static rtx fold_rtx ();
  566. static rtx equiv_constant ();
  567. static void record_jump_cond ();
  568. static void note_mem_written ();
  569. static int cse_rtx_addr_varies_p ();
  570. static enum rtx_code find_comparison_args ();
  571. static void cse_insn ();
  572. static void cse_set_around_loop ();
  573.  
  574. /* Return an estimate of the cost of computing rtx X.
  575.    One use is in cse, to decide which expression to keep in the hash table.
  576.    Another is in rtl generation, to pick the cheapest way to multiply.
  577.    Other uses like the latter are expected in the future.  */
  578.  
  579. /* Return the right cost to give to an operation
  580.    to make the cost of the corresponding register-to-register instruction
  581.    N times that of a fast register-to-register instruction.  */
  582.  
  583. #define COSTS_N_INSNS(N) ((N) * 4 - 2)
  584.  
  585. int
  586. rtx_cost (x, outer_code)
  587.      rtx x;
  588.      enum rtx_code outer_code;
  589. {
  590.   register int i, j;
  591.   register enum rtx_code code;
  592.   register char *fmt;
  593.   register int total;
  594.  
  595.   if (x == 0)
  596.     return 0;
  597.  
  598.   /* Compute the default costs of certain things.
  599.      Note that RTX_COSTS can override the defaults.  */
  600.  
  601.   code = GET_CODE (x);
  602.   switch (code)
  603.     {
  604.     case MULT:
  605.       /* Count multiplication by 2**n as a shift,
  606.      because if we are considering it, we would output it as a shift.  */
  607.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  608.       && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
  609.     total = 2;
  610.       else
  611.     total = COSTS_N_INSNS (5);
  612.       break;
  613.     case DIV:
  614.     case UDIV:
  615.     case MOD:
  616.     case UMOD:
  617.       total = COSTS_N_INSNS (7);
  618.       break;
  619.     case USE:
  620.       /* Used in loop.c and combine.c as a marker.  */
  621.       total = 0;
  622.       break;
  623.     case ASM_OPERANDS:
  624.       /* We don't want these to be used in substitutions because
  625.      we have no way of validating the resulting insn.  So assign
  626.      anything containing an ASM_OPERANDS a very high cost.  */
  627.       total = 1000;
  628.       break;
  629.     default:
  630.       total = 2;
  631.     }
  632.  
  633.   switch (code)
  634.     {
  635.     case REG:
  636.       return 1;
  637.     case SUBREG:
  638.       /* If we can't tie these modes, make this expensive.  The larger
  639.      the mode, the more expensive it is.  */
  640.       if (! MODES_TIEABLE_P (GET_MODE (x), GET_MODE (SUBREG_REG (x))))
  641.     return COSTS_N_INSNS (2
  642.                   + GET_MODE_SIZE (GET_MODE (x)) / UNITS_PER_WORD);
  643.       return 2;
  644. #ifdef RTX_COSTS
  645.       RTX_COSTS (x, code, outer_code);
  646. #endif 
  647.       CONST_COSTS (x, code, outer_code);
  648.     }
  649.  
  650.   /* Sum the costs of the sub-rtx's, plus cost of this operation,
  651.      which is already in total.  */
  652.  
  653.   fmt = GET_RTX_FORMAT (code);
  654.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  655.     if (fmt[i] == 'e')
  656.       total += rtx_cost (XEXP (x, i), code);
  657.     else if (fmt[i] == 'E')
  658.       for (j = 0; j < XVECLEN (x, i); j++)
  659.     total += rtx_cost (XVECEXP (x, i, j), code);
  660.  
  661.   return total;
  662. }
  663.  
  664. /* Clear the hash table and initialize each register with its own quantity,
  665.    for a new basic block.  */
  666.  
  667. static void
  668. new_basic_block ()
  669. {
  670.   register int i;
  671.  
  672.   next_qty = max_reg;
  673.  
  674.   bzero (reg_tick, max_reg * sizeof (int));
  675.  
  676.   bcopy (all_minus_one, reg_in_table, max_reg * sizeof (int));
  677.   bcopy (consec_ints, reg_qty, max_reg * sizeof (int));
  678.   CLEAR_HARD_REG_SET (hard_regs_in_table);
  679.  
  680.   /* The per-quantity values used to be initialized here, but it is
  681.      much faster to initialize each as it is made in `make_new_qty'.  */
  682.  
  683.   for (i = 0; i < NBUCKETS; i++)
  684.     {
  685.       register struct table_elt *this, *next;
  686.       for (this = table[i]; this; this = next)
  687.     {
  688.       next = this->next_same_hash;
  689.       free_element (this);
  690.     }
  691.     }
  692.  
  693.   bzero (table, sizeof table);
  694.  
  695.   prev_insn = 0;
  696.  
  697. #ifdef HAVE_cc0
  698.   prev_insn_cc0 = 0;
  699. #endif
  700. }
  701.  
  702. /* Say that register REG contains a quantity not in any register before
  703.    and initialize that quantity.  */
  704.  
  705. static void
  706. make_new_qty (reg)
  707.      register int reg;
  708. {
  709.   register int q;
  710.  
  711.   if (next_qty >= max_qty)
  712.     abort ();
  713.  
  714.   q = reg_qty[reg] = next_qty++;
  715.   qty_first_reg[q] = reg;
  716.   qty_last_reg[q] = reg;
  717.   qty_const[q] = qty_const_insn[q] = 0;
  718.   qty_comparison_code[q] = UNKNOWN;
  719.  
  720.   reg_next_eqv[reg] = reg_prev_eqv[reg] = -1;
  721. }
  722.  
  723. /* Make reg NEW equivalent to reg OLD.
  724.    OLD is not changing; NEW is.  */
  725.  
  726. static void
  727. make_regs_eqv (new, old)
  728.      register int new, old;
  729. {
  730.   register int lastr, firstr;
  731.   register int q = reg_qty[old];
  732. #ifdef MPW_C
  733.   int t1 = 0, t2, t3;
  734. #endif
  735.  
  736.   /* Nothing should become eqv until it has a "non-invalid" qty number.  */
  737.   if (! REGNO_QTY_VALID_P (old))
  738.     abort ();
  739.  
  740.   reg_qty[new] = q;
  741.   firstr = qty_first_reg[q];
  742.   lastr = qty_last_reg[q];
  743.  
  744.   /* Prefer fixed hard registers to anything.  Prefer pseudo regs to other
  745.      hard regs.  Among pseudos, if NEW will live longer than any other reg
  746.      of the same qty, and that is beyond the current basic block,
  747.      make it the new canonical replacement for this qty.  */
  748. #ifdef MPW_C
  749.   if (! (firstr < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (firstr))) {
  750.     if(new >= FIRST_PSEUDO_REGISTER || REGNO_REG_CLASS (new) != NO_REGS) {
  751.       if(new < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (new)) {t1 = 1;goto done;}
  752.       
  753.       if(new >= FIRST_PSEUDO_REGISTER) {
  754.           if(firstr < FIRST_PSEUDO_REGISTER) {t1 = 1;goto done;}
  755.     t2 = regno_last_uid[new];
  756.     t3 = regno_first_uid[new];
  757.     if(uid_cuid[t2] > cse_basic_block_end
  758.                || (uid_cuid[t3]
  759.                < cse_basic_block_start)) {
  760.       t2 = regno_last_uid[new];
  761.       t3 = regno_last_uid[firstr];
  762.       if(uid_cuid[t2] > uid_cuid[t3]) {t1 = 1;goto done;}
  763.     }
  764.       }
  765.     }
  766.   }
  767. done:
  768.   if(t1) 
  769. #else
  770.   if (! (firstr < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (firstr))
  771.       /* Certain fixed registers might be of the class NO_REGS.  This means
  772.      that not only can they not be allocated by the compiler, but
  773.      they cannot be used in substitutions or canonicalizations
  774.      either.  */
  775.       && (new >= FIRST_PSEUDO_REGISTER || REGNO_REG_CLASS (new) != NO_REGS)
  776.       && ((new < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (new))
  777.       || (new >= FIRST_PSEUDO_REGISTER
  778.           && (firstr < FIRST_PSEUDO_REGISTER
  779.           || ((uid_cuid[regno_last_uid[new]] > cse_basic_block_end
  780.                || (uid_cuid[regno_first_uid[new]]
  781.                < cse_basic_block_start))
  782.               && (uid_cuid[regno_last_uid[new]]
  783.               > uid_cuid[regno_last_uid[firstr]]))))))
  784. #endif
  785.     {
  786.       reg_prev_eqv[firstr] = new;
  787.       reg_next_eqv[new] = firstr;
  788.       reg_prev_eqv[new] = -1;
  789.       qty_first_reg[q] = new;
  790.     }
  791.   else
  792.     {
  793.       /* If NEW is a hard reg (known to be non-fixed), insert at end.
  794.      Otherwise, insert before any non-fixed hard regs that are at the
  795.      end.  Registers of class NO_REGS cannot be used as an
  796.      equivalent for anything.  */
  797.       while (lastr < FIRST_PSEUDO_REGISTER && reg_prev_eqv[lastr] >= 0
  798.          && (REGNO_REG_CLASS (lastr) == NO_REGS || ! FIXED_REGNO_P (lastr))
  799.          && new >= FIRST_PSEUDO_REGISTER)
  800.     lastr = reg_prev_eqv[lastr];
  801.       reg_next_eqv[new] = reg_next_eqv[lastr];
  802.       if (reg_next_eqv[lastr] >= 0)
  803.     reg_prev_eqv[reg_next_eqv[lastr]] = new;
  804.       else
  805.     qty_last_reg[q] = new;
  806.       reg_next_eqv[lastr] = new;
  807.       reg_prev_eqv[new] = lastr;
  808.     }
  809. }
  810.  
  811. /* Remove REG from its equivalence class.  */
  812.  
  813. static void
  814. delete_reg_equiv (reg)
  815.      register int reg;
  816. {
  817.   register int n = reg_next_eqv[reg];
  818.   register int p = reg_prev_eqv[reg];
  819.   register int q = reg_qty[reg];
  820.  
  821.   /* If invalid, do nothing.  N and P above are undefined in that case.  */
  822.   if (q == reg)
  823.     return;
  824.  
  825.   if (n != -1)
  826.     reg_prev_eqv[n] = p;
  827.   else
  828.     qty_last_reg[q] = p;
  829.   if (p != -1)
  830.     reg_next_eqv[p] = n;
  831.   else
  832.     qty_first_reg[q] = n;
  833.  
  834.   reg_qty[reg] = reg;
  835. }
  836.  
  837. /* Remove any invalid expressions from the hash table
  838.    that refer to any of the registers contained in expression X.
  839.  
  840.    Make sure that newly inserted references to those registers
  841.    as subexpressions will be considered valid.
  842.  
  843.    mention_regs is not called when a register itself
  844.    is being stored in the table.
  845.  
  846.    Return 1 if we have done something that may have changed the hash code
  847.    of X.  */
  848.  
  849. static int
  850. mention_regs (x)
  851.      rtx x;
  852. {
  853.   register enum rtx_code code;
  854.   register int i, j;
  855.   register char *fmt;
  856.   register int changed = 0;
  857.  
  858.   if (x == 0)
  859.     return 0;
  860.  
  861.   code = GET_CODE (x);
  862.   if (code == REG)
  863.     {
  864.       register int regno = REGNO (x);
  865.       register int endregno
  866.     = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1
  867.            : HARD_REGNO_NREGS (regno, GET_MODE (x)));
  868.       int i;
  869.  
  870.       for (i = regno; i < endregno; i++)
  871.     {
  872.       if (reg_in_table[i] >= 0 && reg_in_table[i] != reg_tick[i])
  873.         remove_invalid_refs (i);
  874.  
  875.       reg_in_table[i] = reg_tick[i];
  876.     }
  877.  
  878.       return 0;
  879.     }
  880.  
  881.   /* If X is a comparison or a COMPARE and either operand is a register
  882.      that does not have a quantity, give it one.  This is so that a later
  883.      call to record_jump_equiv won't cause X to be assigned a different
  884.      hash code and not found in the table after that call.
  885.  
  886.      It is not necessary to do this here, since rehash_using_reg can
  887.      fix up the table later, but doing this here eliminates the need to
  888.      call that expensive function in the most common case where the only
  889.      use of the register is in the comparison.  */
  890.  
  891.   if (code == COMPARE || GET_RTX_CLASS (code) == '<')
  892.     {
  893.       if (GET_CODE (XEXP (x, 0)) == REG
  894.       && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))))
  895.     if (insert_regs (XEXP (x, 0), NULL_PTR, 0))
  896.       {
  897.         rehash_using_reg (XEXP (x, 0));
  898.         changed = 1;
  899.       }
  900.  
  901.       if (GET_CODE (XEXP (x, 1)) == REG
  902.       && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 1))))
  903.     if (insert_regs (XEXP (x, 1), NULL_PTR, 0))
  904.       {
  905.         rehash_using_reg (XEXP (x, 1));
  906.         changed = 1;
  907.       }
  908.     }
  909.  
  910.   fmt = GET_RTX_FORMAT (code);
  911.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  912.     if (fmt[i] == 'e')
  913.       changed |= mention_regs (XEXP (x, i));
  914.     else if (fmt[i] == 'E')
  915.       for (j = 0; j < XVECLEN (x, i); j++)
  916.     changed |= mention_regs (XVECEXP (x, i, j));
  917.  
  918.   return changed;
  919. }
  920.  
  921. /* Update the register quantities for inserting X into the hash table
  922.    with a value equivalent to CLASSP.
  923.    (If the class does not contain a REG, it is irrelevant.)
  924.    If MODIFIED is nonzero, X is a destination; it is being modified.
  925.    Note that delete_reg_equiv should be called on a register
  926.    before insert_regs is done on that register with MODIFIED != 0.
  927.  
  928.    Nonzero value means that elements of reg_qty have changed
  929.    so X's hash code may be different.  */
  930.  
  931. static int
  932. insert_regs (x, classp, modified)
  933.      rtx x;
  934.      struct table_elt *classp;
  935.      int modified;
  936. {
  937.   if (GET_CODE (x) == REG)
  938.     {
  939.       register int regno = REGNO (x);
  940.  
  941.       if (modified
  942.       || ! (REGNO_QTY_VALID_P (regno)
  943.         && qty_mode[reg_qty[regno]] == GET_MODE (x)))
  944.     {
  945.       if (classp)
  946.         for (classp = classp->first_same_value;
  947.          classp != 0;
  948.          classp = classp->next_same_value)
  949.           if (GET_CODE (classp->exp) == REG
  950.           && GET_MODE (classp->exp) == GET_MODE (x))
  951.         {
  952.           make_regs_eqv (regno, REGNO (classp->exp));
  953.           return 1;
  954.         }
  955.  
  956.       make_new_qty (regno);
  957.       qty_mode[reg_qty[regno]] = GET_MODE (x);
  958.       return 1;
  959.     }
  960.     }
  961.  
  962.   /* If X is a SUBREG, we will likely be inserting the inner register in the
  963.      table.  If that register doesn't have an assigned quantity number at
  964.      this point but does later, the insertion that we will be doing now will
  965.      not be accessible because its hash code will have changed.  So assign
  966.      a quantity number now.  */
  967.  
  968.   else if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == REG
  969.        && ! REGNO_QTY_VALID_P (REGNO (SUBREG_REG (x))))
  970.     {
  971.       insert_regs (SUBREG_REG (x), NULL_PTR, 0);
  972.       mention_regs (SUBREG_REG (x));
  973.       return 1;
  974.     }
  975.   else
  976.     return mention_regs (x);
  977. }
  978.  
  979. /* Look in or update the hash table.  */
  980.  
  981. /* Put the element ELT on the list of free elements.  */
  982.  
  983. static void
  984. free_element (elt)
  985.      struct table_elt *elt;
  986. {
  987.   elt->next_same_hash = free_element_chain;
  988.   free_element_chain = elt;
  989. }
  990.  
  991. /* Return an element that is free for use.  */
  992.  
  993. static struct table_elt *
  994. get_element ()
  995. {
  996.   struct table_elt *elt = free_element_chain;
  997.   if (elt)
  998.     {
  999.       free_element_chain = elt->next_same_hash;
  1000.       return elt;
  1001.     }
  1002.   n_elements_made++;
  1003.   return (struct table_elt *) oballoc (sizeof (struct table_elt));
  1004. }
  1005.  
  1006. /* Remove table element ELT from use in the table.
  1007.    HASH is its hash code, made using the HASH macro.
  1008.    It's an argument because often that is known in advance
  1009.    and we save much time not recomputing it.  */
  1010.  
  1011. static void
  1012. remove_from_table (elt, hash)
  1013.      register struct table_elt *elt;
  1014.      int hash;
  1015. {
  1016.   if (elt == 0)
  1017.     return;
  1018.  
  1019.   /* Mark this element as removed.  See cse_insn.  */
  1020.   elt->first_same_value = 0;
  1021.  
  1022.   /* Remove the table element from its equivalence class.  */
  1023.      
  1024.   {
  1025.     register struct table_elt *prev = elt->prev_same_value;
  1026.     register struct table_elt *next = elt->next_same_value;
  1027.  
  1028.     if (next) next->prev_same_value = prev;
  1029.  
  1030.     if (prev)
  1031.       prev->next_same_value = next;
  1032.     else
  1033.       {
  1034.     register struct table_elt *newfirst = next;
  1035.     while (next)
  1036.       {
  1037.         next->first_same_value = newfirst;
  1038.         next = next->next_same_value;
  1039.       }
  1040.       }
  1041.   }
  1042.  
  1043.   /* Remove the table element from its hash bucket.  */
  1044.  
  1045.   {
  1046.     register struct table_elt *prev = elt->prev_same_hash;
  1047.     register struct table_elt *next = elt->next_same_hash;
  1048.  
  1049.     if (next) next->prev_same_hash = prev;
  1050.  
  1051.     if (prev)
  1052.       prev->next_same_hash = next;
  1053.     else if (table[hash] == elt)
  1054.       table[hash] = next;
  1055.     else
  1056.       {
  1057.     /* This entry is not in the proper hash bucket.  This can happen
  1058.        when two classes were merged by `merge_equiv_classes'.  Search
  1059.        for the hash bucket that it heads.  This happens only very
  1060.        rarely, so the cost is acceptable.  */
  1061.     for (hash = 0; hash < NBUCKETS; hash++)
  1062.       if (table[hash] == elt)
  1063.         table[hash] = next;
  1064.       }
  1065.   }
  1066.  
  1067.   /* Remove the table element from its related-value circular chain.  */
  1068.  
  1069.   if (elt->related_value != 0 && elt->related_value != elt)
  1070.     {
  1071.       register struct table_elt *p = elt->related_value;
  1072.       while (p->related_value != elt)
  1073.     p = p->related_value;
  1074.       p->related_value = elt->related_value;
  1075.       if (p->related_value == p)
  1076.     p->related_value = 0;
  1077.     }
  1078.  
  1079.   free_element (elt);
  1080. }
  1081.  
  1082. /* Look up X in the hash table and return its table element,
  1083.    or 0 if X is not in the table.
  1084.  
  1085.    MODE is the machine-mode of X, or if X is an integer constant
  1086.    with VOIDmode then MODE is the mode with which X will be used.
  1087.  
  1088.    Here we are satisfied to find an expression whose tree structure
  1089.    looks like X.  */
  1090.  
  1091. static struct table_elt *
  1092. lookup (x, hash, mode)
  1093.      rtx x;
  1094.      int hash;
  1095.      enum machine_mode mode;
  1096. {
  1097.   register struct table_elt *p;
  1098.  
  1099.   for (p = table[hash]; p; p = p->next_same_hash)
  1100.     if (mode == p->mode && ((x == p->exp && GET_CODE (x) == REG)
  1101.                 || exp_equiv_p (x, p->exp, GET_CODE (x) != REG, 0)))
  1102.       return p;
  1103.  
  1104.   return 0;
  1105. }
  1106.  
  1107. /* Like `lookup' but don't care whether the table element uses invalid regs.
  1108.    Also ignore discrepancies in the machine mode of a register.  */
  1109.  
  1110. static struct table_elt *
  1111. lookup_for_remove (x, hash, mode)
  1112.      rtx x;
  1113.      int hash;
  1114.      enum machine_mode mode;
  1115. {
  1116.   register struct table_elt *p;
  1117.  
  1118.   if (GET_CODE (x) == REG)
  1119.     {
  1120.       int regno = REGNO (x);
  1121.       /* Don't check the machine mode when comparing registers;
  1122.      invalidating (REG:SI 0) also invalidates (REG:DF 0).  */
  1123.       for (p = table[hash]; p; p = p->next_same_hash)
  1124.     if (GET_CODE (p->exp) == REG
  1125.         && REGNO (p->exp) == regno)
  1126.       return p;
  1127.     }
  1128.   else
  1129.     {
  1130.       for (p = table[hash]; p; p = p->next_same_hash)
  1131.     if (mode == p->mode && (x == p->exp || exp_equiv_p (x, p->exp, 0, 0)))
  1132.       return p;
  1133.     }
  1134.  
  1135.   return 0;
  1136. }
  1137.  
  1138. /* Look for an expression equivalent to X and with code CODE.
  1139.    If one is found, return that expression.  */
  1140.  
  1141. static rtx
  1142. lookup_as_function (x, code)
  1143.      rtx x;
  1144.      enum rtx_code code;
  1145. {
  1146.   register struct table_elt *p = lookup (x, safe_hash (x, VOIDmode) % NBUCKETS,
  1147.                      GET_MODE (x));
  1148.   if (p == 0)
  1149.     return 0;
  1150.  
  1151.   for (p = p->first_same_value; p; p = p->next_same_value)
  1152.     {
  1153.       if (GET_CODE (p->exp) == code
  1154.       /* Make sure this is a valid entry in the table.  */
  1155.       && exp_equiv_p (p->exp, p->exp, 1, 0))
  1156.     return p->exp;
  1157.     }
  1158.   
  1159.   return 0;
  1160. }
  1161.  
  1162. /* Insert X in the hash table, assuming HASH is its hash code
  1163.    and CLASSP is an element of the class it should go in
  1164.    (or 0 if a new class should be made).
  1165.    It is inserted at the proper position to keep the class in
  1166.    the order cheapest first.
  1167.  
  1168.    MODE is the machine-mode of X, or if X is an integer constant
  1169.    with VOIDmode then MODE is the mode with which X will be used.
  1170.  
  1171.    For elements of equal cheapness, the most recent one
  1172.    goes in front, except that the first element in the list
  1173.    remains first unless a cheaper element is added.  The order of
  1174.    pseudo-registers does not matter, as canon_reg will be called to
  1175.    find the cheapest when a register is retrieved from the table.
  1176.  
  1177.    The in_memory field in the hash table element is set to 0.
  1178.    The caller must set it nonzero if appropriate.
  1179.  
  1180.    You should call insert_regs (X, CLASSP, MODIFY) before calling here,
  1181.    and if insert_regs returns a nonzero value
  1182.    you must then recompute its hash code before calling here.
  1183.  
  1184.    If necessary, update table showing constant values of quantities.  */
  1185.  
  1186. #define CHEAPER(X,Y)   ((X)->cost < (Y)->cost)
  1187.  
  1188. static struct table_elt *
  1189. insert (x, classp, hash, mode)
  1190.      register rtx x;
  1191.      register struct table_elt *classp;
  1192.      int hash;
  1193.      enum machine_mode mode;
  1194. {
  1195.   register struct table_elt *elt;
  1196.  
  1197.   /* If X is a register and we haven't made a quantity for it,
  1198.      something is wrong.  */
  1199.   if (GET_CODE (x) == REG && ! REGNO_QTY_VALID_P (REGNO (x)))
  1200.     abort ();
  1201.  
  1202.   /* If X is a hard register, show it is being put in the table.  */
  1203.   if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
  1204.     {
  1205.       int regno = REGNO (x);
  1206.       int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
  1207.       int i;
  1208.  
  1209.       for (i = regno; i < endregno; i++)
  1210.         SET_HARD_REG_BIT (hard_regs_in_table, i);
  1211.     }
  1212.  
  1213.  
  1214.   /* Put an element for X into the right hash bucket.  */
  1215.  
  1216.   elt = get_element ();
  1217.   elt->exp = x;
  1218.   elt->cost = COST (x);
  1219.   elt->next_same_value = 0;
  1220.   elt->prev_same_value = 0;
  1221.   elt->next_same_hash = table[hash];
  1222.   elt->prev_same_hash = 0;
  1223.   elt->related_value = 0;
  1224.   elt->in_memory = 0;
  1225.   elt->mode = mode;
  1226.   elt->is_const = (CONSTANT_P (x)
  1227.            /* GNU C++ takes advantage of this for `this'
  1228.               (and other const values).  */
  1229.            || (RTX_UNCHANGING_P (x)
  1230.                && GET_CODE (x) == REG
  1231.                && REGNO (x) >= FIRST_PSEUDO_REGISTER)
  1232.            || FIXED_BASE_PLUS_P (x));
  1233.  
  1234.   if (table[hash])
  1235.     table[hash]->prev_same_hash = elt;
  1236.   table[hash] = elt;
  1237.  
  1238.   /* Put it into the proper value-class.  */
  1239.   if (classp)
  1240.     {
  1241.       classp = classp->first_same_value;
  1242.       if (CHEAPER (elt, classp))
  1243.     /* Insert at the head of the class */
  1244.     {
  1245.       register struct table_elt *p;
  1246.       elt->next_same_value = classp;
  1247.       classp->prev_same_value = elt;
  1248.       elt->first_same_value = elt;
  1249.  
  1250.       for (p = classp; p; p = p->next_same_value)
  1251.         p->first_same_value = elt;
  1252.     }
  1253.       else
  1254.     {
  1255.       /* Insert not at head of the class.  */
  1256.       /* Put it after the last element cheaper than X.  */
  1257.       register struct table_elt *p, *next;
  1258.       for (p = classp; (next = p->next_same_value) && CHEAPER (next, elt);
  1259.            p = next);
  1260.       /* Put it after P and before NEXT.  */
  1261.       elt->next_same_value = next;
  1262.       if (next)
  1263.         next->prev_same_value = elt;
  1264.       elt->prev_same_value = p;
  1265.       p->next_same_value = elt;
  1266.       elt->first_same_value = classp;
  1267.     }
  1268.     }
  1269.   else
  1270.     elt->first_same_value = elt;
  1271.  
  1272.   /* If this is a constant being set equivalent to a register or a register
  1273.      being set equivalent to a constant, note the constant equivalence.
  1274.  
  1275.      If this is a constant, it cannot be equivalent to a different constant,
  1276.      and a constant is the only thing that can be cheaper than a register.  So
  1277.      we know the register is the head of the class (before the constant was
  1278.      inserted).
  1279.  
  1280.      If this is a register that is not already known equivalent to a
  1281.      constant, we must check the entire class.
  1282.  
  1283.      If this is a register that is already known equivalent to an insn,
  1284.      update `qty_const_insn' to show that `this_insn' is the latest
  1285.      insn making that quantity equivalent to the constant.  */
  1286.  
  1287.   if (elt->is_const && classp && GET_CODE (classp->exp) == REG)
  1288.     {
  1289.       qty_const[reg_qty[REGNO (classp->exp)]]
  1290.     = gen_lowpart_if_possible (qty_mode[reg_qty[REGNO (classp->exp)]], x);
  1291.       qty_const_insn[reg_qty[REGNO (classp->exp)]] = this_insn;
  1292.     }
  1293.  
  1294.   else if (GET_CODE (x) == REG && classp && ! qty_const[reg_qty[REGNO (x)]])
  1295.     {
  1296.       register struct table_elt *p;
  1297.  
  1298.       for (p = classp; p != 0; p = p->next_same_value)
  1299.     {
  1300.       if (p->is_const)
  1301.         {
  1302.           qty_const[reg_qty[REGNO (x)]]
  1303.         = gen_lowpart_if_possible (GET_MODE (x), p->exp);
  1304.           qty_const_insn[reg_qty[REGNO (x)]] = this_insn;
  1305.           break;
  1306.         }
  1307.     }
  1308.     }
  1309.  
  1310.   else if (GET_CODE (x) == REG && qty_const[reg_qty[REGNO (x)]]
  1311.        && GET_MODE (x) == qty_mode[reg_qty[REGNO (x)]])
  1312.     qty_const_insn[reg_qty[REGNO (x)]] = this_insn;
  1313.  
  1314.   /* If this is a constant with symbolic value,
  1315.      and it has a term with an explicit integer value,
  1316.      link it up with related expressions.  */
  1317.   if (GET_CODE (x) == CONST)
  1318.     {
  1319.       rtx subexp = get_related_value (x);
  1320.       int subhash;
  1321.       struct table_elt *subelt, *subelt_prev;
  1322.  
  1323.       if (subexp != 0)
  1324.     {
  1325.       /* Get the integer-free subexpression in the hash table.  */
  1326.       subhash = safe_hash (subexp, mode) % NBUCKETS;
  1327.       subelt = lookup (subexp, subhash, mode);
  1328.       if (subelt == 0)
  1329.         subelt = insert (subexp, NULL_PTR, subhash, mode);
  1330.       /* Initialize SUBELT's circular chain if it has none.  */
  1331.       if (subelt->related_value == 0)
  1332.         subelt->related_value = subelt;
  1333.       /* Find the element in the circular chain that precedes SUBELT.  */
  1334.       subelt_prev = subelt;
  1335.       while (subelt_prev->related_value != subelt)
  1336.         subelt_prev = subelt_prev->related_value;
  1337.       /* Put new ELT into SUBELT's circular chain just before SUBELT.
  1338.          This way the element that follows SUBELT is the oldest one.  */
  1339.       elt->related_value = subelt_prev->related_value;
  1340.       subelt_prev->related_value = elt;
  1341.     }
  1342.     }
  1343.  
  1344.   return elt;
  1345. }
  1346.  
  1347. /* Given two equivalence classes, CLASS1 and CLASS2, put all the entries from
  1348.    CLASS2 into CLASS1.  This is done when we have reached an insn which makes
  1349.    the two classes equivalent.
  1350.  
  1351.    CLASS1 will be the surviving class; CLASS2 should not be used after this
  1352.    call.
  1353.  
  1354.    Any invalid entries in CLASS2 will not be copied.  */
  1355.  
  1356. static void
  1357. merge_equiv_classes (class1, class2)
  1358.      struct table_elt *class1, *class2;
  1359. {
  1360.   struct table_elt *elt, *next, *new;
  1361.  
  1362.   /* Ensure we start with the head of the classes.  */
  1363.   class1 = class1->first_same_value;
  1364.   class2 = class2->first_same_value;
  1365.  
  1366.   /* If they were already equal, forget it.  */
  1367.   if (class1 == class2)
  1368.     return;
  1369.  
  1370.   for (elt = class2; elt; elt = next)
  1371.     {
  1372.       int hash;
  1373.       rtx exp = elt->exp;
  1374.       enum machine_mode mode = elt->mode;
  1375.  
  1376.       next = elt->next_same_value;
  1377.  
  1378.       /* Remove old entry, make a new one in CLASS1's class.
  1379.      Don't do this for invalid entries as we cannot find their
  1380.      hash code (it also isn't necessary). */
  1381.       if (GET_CODE (exp) == REG || exp_equiv_p (exp, exp, 1, 0))
  1382.     {
  1383.       hash_arg_in_memory = 0;
  1384.       hash_arg_in_struct = 0;
  1385.       hash = HASH (exp, mode);
  1386.           
  1387.       if (GET_CODE (exp) == REG)
  1388.         delete_reg_equiv (REGNO (exp));
  1389.           
  1390.       remove_from_table (elt, hash);
  1391.  
  1392.       if (insert_regs (exp, class1, 0))
  1393.         hash = HASH (exp, mode);
  1394.       new = insert (exp, class1, hash, mode);
  1395.       new->in_memory = hash_arg_in_memory;
  1396.       new->in_struct = hash_arg_in_struct;
  1397.     }
  1398.     }
  1399. }
  1400.  
  1401. /* Remove from the hash table, or mark as invalid,
  1402.    all expressions whose values could be altered by storing in X.
  1403.    X is a register, a subreg, or a memory reference with nonvarying address
  1404.    (because, when a memory reference with a varying address is stored in,
  1405.    all memory references are removed by invalidate_memory
  1406.    so specific invalidation is superfluous).
  1407.  
  1408.    A nonvarying address may be just a register or just
  1409.    a symbol reference, or it may be either of those plus
  1410.    a numeric offset.  */
  1411.  
  1412. static void
  1413. invalidate (x)
  1414.      rtx x;
  1415. {
  1416.   register int i;
  1417.   register struct table_elt *p;
  1418.   register rtx base;
  1419.   register HOST_WIDE_INT start, end;
  1420.  
  1421.   /* If X is a register, dependencies on its contents
  1422.      are recorded through the qty number mechanism.
  1423.      Just change the qty number of the register,
  1424.      mark it as invalid for expressions that refer to it,
  1425.      and remove it itself.  */
  1426.  
  1427.   if (GET_CODE (x) == REG)
  1428.     {
  1429.       register int regno = REGNO (x);
  1430.       register int hash = HASH (x, GET_MODE (x));
  1431.  
  1432.       /* Remove REGNO from any quantity list it might be on and indicate
  1433.      that it's value might have changed.  If it is a pseudo, remove its
  1434.      entry from the hash table.
  1435.  
  1436.      For a hard register, we do the first two actions above for any
  1437.      additional hard registers corresponding to X.  Then, if any of these
  1438.      registers are in the table, we must remove any REG entries that
  1439.      overlap these registers.  */
  1440.  
  1441.       delete_reg_equiv (regno);
  1442.       reg_tick[regno]++;
  1443.  
  1444.       if (regno >= FIRST_PSEUDO_REGISTER)
  1445.     remove_from_table (lookup_for_remove (x, hash, GET_MODE (x)), hash);
  1446.       else
  1447.     {
  1448.       int in_table = TEST_HARD_REG_BIT (hard_regs_in_table, regno);
  1449.       int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
  1450.       int tregno, tendregno;
  1451.       register struct table_elt *p, *next;
  1452.  
  1453.       CLEAR_HARD_REG_BIT (hard_regs_in_table, regno);
  1454.  
  1455.       for (i = regno + 1; i < endregno; i++)
  1456.         {
  1457.           in_table |= TEST_HARD_REG_BIT (hard_regs_in_table, i);
  1458.           CLEAR_HARD_REG_BIT (hard_regs_in_table, i);
  1459.           delete_reg_equiv (i);
  1460.           reg_tick[i]++;
  1461.         }
  1462.  
  1463.       if (in_table)
  1464.         for (hash = 0; hash < NBUCKETS; hash++)
  1465.           for (p = table[hash]; p; p = next)
  1466.         {
  1467.           next = p->next_same_hash;
  1468.  
  1469.           if (GET_CODE (p->exp) != REG
  1470.               || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
  1471.             continue;
  1472.  
  1473.           tregno = REGNO (p->exp);
  1474.           tendregno
  1475.             = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (p->exp));
  1476.           if (tendregno > regno && tregno < endregno)
  1477.           remove_from_table (p, hash);
  1478.         }
  1479.     }
  1480.  
  1481.       return;
  1482.     }
  1483.  
  1484.   if (GET_CODE (x) == SUBREG)
  1485.     {
  1486.       if (GET_CODE (SUBREG_REG (x)) != REG)
  1487.     abort ();
  1488.       invalidate (SUBREG_REG (x));
  1489.       return;
  1490.     }
  1491.  
  1492.   /* X is not a register; it must be a memory reference with
  1493.      a nonvarying address.  Remove all hash table elements
  1494.      that refer to overlapping pieces of memory.  */
  1495.  
  1496.   if (GET_CODE (x) != MEM)
  1497.     abort ();
  1498.   base = XEXP (x, 0);
  1499.   start = 0;
  1500.  
  1501.   /* Registers with nonvarying addresses usually have constant equivalents;
  1502.      but the frame pointer register is also possible.  */
  1503.   if (GET_CODE (base) == REG
  1504.       && REGNO_QTY_VALID_P (REGNO (base))
  1505.       && qty_mode[reg_qty[REGNO (base)]] == GET_MODE (base)
  1506.       && qty_const[reg_qty[REGNO (base)]] != 0)
  1507.     base = qty_const[reg_qty[REGNO (base)]];
  1508.   else if (GET_CODE (base) == PLUS
  1509.        && GET_CODE (XEXP (base, 1)) == CONST_INT
  1510.        && GET_CODE (XEXP (base, 0)) == REG
  1511.        && REGNO_QTY_VALID_P (REGNO (XEXP (base, 0)))
  1512.        && (qty_mode[reg_qty[REGNO (XEXP (base, 0))]]
  1513.            == GET_MODE (XEXP (base, 0)))
  1514.        && qty_const[reg_qty[REGNO (XEXP (base, 0))]])
  1515.     {
  1516.       start = INTVAL (XEXP (base, 1));
  1517.       base = qty_const[reg_qty[REGNO (XEXP (base, 0))]];
  1518.     }
  1519.  
  1520.   if (GET_CODE (base) == CONST)
  1521.     base = XEXP (base, 0);
  1522.   if (GET_CODE (base) == PLUS
  1523.       && GET_CODE (XEXP (base, 1)) == CONST_INT)
  1524.     {
  1525.       start += INTVAL (XEXP (base, 1));
  1526.       base = XEXP (base, 0);
  1527.     }
  1528.  
  1529.   end = start + GET_MODE_SIZE (GET_MODE (x));
  1530.   for (i = 0; i < NBUCKETS; i++)
  1531.     {
  1532.       register struct table_elt *next;
  1533.       for (p = table[i]; p; p = next)
  1534.     {
  1535.       next = p->next_same_hash;
  1536.       if (refers_to_mem_p (p->exp, base, start, end))
  1537.         remove_from_table (p, i);
  1538.     }
  1539.     }
  1540. }
  1541.  
  1542. /* Remove all expressions that refer to register REGNO,
  1543.    since they are already invalid, and we are about to
  1544.    mark that register valid again and don't want the old
  1545.    expressions to reappear as valid.  */
  1546.  
  1547. static void
  1548. remove_invalid_refs (regno)
  1549.      int regno;
  1550. {
  1551.   register int i;
  1552.   register struct table_elt *p, *next;
  1553.  
  1554.   for (i = 0; i < NBUCKETS; i++)
  1555.     for (p = table[i]; p; p = next)
  1556.       {
  1557.     next = p->next_same_hash;
  1558.     if (GET_CODE (p->exp) != REG
  1559.         && refers_to_regno_p (regno, regno + 1, p->exp, NULL_PTR))
  1560.       remove_from_table (p, i);
  1561.       }
  1562. }
  1563.  
  1564. /* Recompute the hash codes of any valid entries in the hash table that
  1565.    reference X, if X is a register, or SUBREG_REG (X) if X is a SUBREG.
  1566.  
  1567.    This is called when we make a jump equivalence.  */
  1568.  
  1569. static void
  1570. rehash_using_reg (x)
  1571.      rtx x;
  1572. {
  1573.   int i;
  1574.   struct table_elt *p, *next;
  1575.   int hash;
  1576.  
  1577.   if (GET_CODE (x) == SUBREG)
  1578.     x = SUBREG_REG (x);
  1579.  
  1580.   /* If X is not a register or if the register is known not to be in any
  1581.      valid entries in the table, we have no work to do.  */
  1582.  
  1583.   if (GET_CODE (x) != REG
  1584.       || reg_in_table[REGNO (x)] < 0
  1585.       || reg_in_table[REGNO (x)] != reg_tick[REGNO (x)])
  1586.     return;
  1587.  
  1588.   /* Scan all hash chains looking for valid entries that mention X.
  1589.      If we find one and it is in the wrong hash chain, move it.  We can skip
  1590.      objects that are registers, since they are handled specially.  */
  1591.  
  1592.   for (i = 0; i < NBUCKETS; i++)
  1593.     for (p = table[i]; p; p = next)
  1594.       {
  1595.     next = p->next_same_hash;
  1596.     if (GET_CODE (p->exp) != REG && reg_mentioned_p (x, p->exp)
  1597.         && exp_equiv_p (p->exp, p->exp, 1, 0)
  1598.         && i != (hash = safe_hash (p->exp, p->mode) % NBUCKETS))
  1599.       {
  1600.         if (p->next_same_hash)
  1601.           p->next_same_hash->prev_same_hash = p->prev_same_hash;
  1602.  
  1603.         if (p->prev_same_hash)
  1604.           p->prev_same_hash->next_same_hash = p->next_same_hash;
  1605.         else
  1606.           table[i] = p->next_same_hash;
  1607.  
  1608.         p->next_same_hash = table[hash];
  1609.         p->prev_same_hash = 0;
  1610.         if (table[hash])
  1611.           table[hash]->prev_same_hash = p;
  1612.         table[hash] = p;
  1613.       }
  1614.       }
  1615. }
  1616.  
  1617. /* Remove from the hash table all expressions that reference memory,
  1618.    or some of them as specified by *WRITES.  */
  1619.  
  1620. static void
  1621. invalidate_memory (writes)
  1622.      struct write_data *writes;
  1623. {
  1624.   register int i;
  1625.   register struct table_elt *p, *next;
  1626.   int all = writes->all;
  1627.   int nonscalar = writes->nonscalar;
  1628.  
  1629.   for (i = 0; i < NBUCKETS; i++)
  1630.     for (p = table[i]; p; p = next)
  1631.       {
  1632.     next = p->next_same_hash;
  1633.     if (p->in_memory
  1634.         && (all
  1635.         || (nonscalar && p->in_struct)
  1636.         || cse_rtx_addr_varies_p (p->exp)))
  1637.       remove_from_table (p, i);
  1638.       }
  1639. }
  1640.  
  1641. /* Remove from the hash table any expression that is a call-clobbered
  1642.    register.  Also update their TICK values.  */
  1643.  
  1644. static void
  1645. invalidate_for_call ()
  1646. {
  1647.   int regno, endregno;
  1648.   int i;
  1649.   int hash;
  1650.   struct table_elt *p, *next;
  1651.   int in_table = 0;
  1652.  
  1653.   /* Go through all the hard registers.  For each that is clobbered in
  1654.      a CALL_INSN, remove the register from quantity chains and update
  1655.      reg_tick if defined.  Also see if any of these registers is currently
  1656.      in the table.  */
  1657.  
  1658.   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  1659.     if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
  1660.       {
  1661.     delete_reg_equiv (regno);
  1662.     if (reg_tick[regno] >= 0)
  1663.       reg_tick[regno]++;
  1664.  
  1665.     in_table |= TEST_HARD_REG_BIT (hard_regs_in_table, regno);
  1666.       }
  1667.  
  1668.   /* In the case where we have no call-clobbered hard registers in the
  1669.      table, we are done.  Otherwise, scan the table and remove any
  1670.      entry that overlaps a call-clobbered register.  */
  1671.  
  1672.   if (in_table)
  1673.     for (hash = 0; hash < NBUCKETS; hash++)
  1674.       for (p = table[hash]; p; p = next)
  1675.     {
  1676.       next = p->next_same_hash;
  1677.  
  1678.       if (GET_CODE (p->exp) != REG
  1679.           || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
  1680.         continue;
  1681.  
  1682.       regno = REGNO (p->exp);
  1683.       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (p->exp));
  1684.  
  1685.       for (i = regno; i < endregno; i++)
  1686.         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
  1687.           {
  1688.         remove_from_table (p, hash);
  1689.         break;
  1690.           }
  1691.     }
  1692. }
  1693.  
  1694. /* Given an expression X of type CONST,
  1695.    and ELT which is its table entry (or 0 if it
  1696.    is not in the hash table),
  1697.    return an alternate expression for X as a register plus integer.
  1698.    If none can be found, return 0.  */
  1699.  
  1700. static rtx
  1701. use_related_value (x, elt)
  1702.      rtx x;
  1703.      struct table_elt *elt;
  1704. {
  1705.   register struct table_elt *relt = 0;
  1706.   register struct table_elt *p, *q;
  1707.   HOST_WIDE_INT offset;
  1708.  
  1709.   /* First, is there anything related known?
  1710.      If we have a table element, we can tell from that.
  1711.      Otherwise, must look it up.  */
  1712.  
  1713.   if (elt != 0 && elt->related_value != 0)
  1714.     relt = elt;
  1715.   else if (elt == 0 && GET_CODE (x) == CONST)
  1716.     {
  1717.       rtx subexp = get_related_value (x);
  1718.       if (subexp != 0)
  1719.     relt = lookup (subexp,
  1720.                safe_hash (subexp, GET_MODE (subexp)) % NBUCKETS,
  1721.                GET_MODE (subexp));
  1722.     }
  1723.  
  1724.   if (relt == 0)
  1725.     return 0;
  1726.  
  1727.   /* Search all related table entries for one that has an
  1728.      equivalent register.  */
  1729.  
  1730.   p = relt;
  1731.   while (1)
  1732.     {
  1733.       /* This loop is strange in that it is executed in two different cases.
  1734.      The first is when X is already in the table.  Then it is searching
  1735.      the RELATED_VALUE list of X's class (RELT).  The second case is when
  1736.      X is not in the table.  Then RELT points to a class for the related
  1737.      value.
  1738.  
  1739.      Ensure that, whatever case we are in, that we ignore classes that have
  1740.      the same value as X.  */
  1741.  
  1742.       if (rtx_equal_p (x, p->exp))
  1743.     q = 0;
  1744.       else
  1745.     for (q = p->first_same_value; q; q = q->next_same_value)
  1746.       if (GET_CODE (q->exp) == REG)
  1747.         break;
  1748.  
  1749.       if (q)
  1750.     break;
  1751.  
  1752.       p = p->related_value;
  1753.  
  1754.       /* We went all the way around, so there is nothing to be found.
  1755.      Alternatively, perhaps RELT was in the table for some other reason
  1756.      and it has no related values recorded.  */
  1757.       if (p == relt || p == 0)
  1758.     break;
  1759.     }
  1760.  
  1761.   if (q == 0)
  1762.     return 0;
  1763.  
  1764.   offset = (get_integer_term (x) - get_integer_term (p->exp));
  1765.   /* Note: OFFSET may be 0 if P->xexp and X are related by commutativity.  */
  1766.   return plus_constant (q->exp, offset);
  1767. }
  1768.  
  1769. /* Hash an rtx.  We are careful to make sure the value is never negative.
  1770.    Equivalent registers hash identically.
  1771.    MODE is used in hashing for CONST_INTs only;
  1772.    otherwise the mode of X is used.
  1773.  
  1774.    Store 1 in do_not_record if any subexpression is volatile.
  1775.  
  1776.    Store 1 in hash_arg_in_memory if X contains a MEM rtx
  1777.    which does not have the RTX_UNCHANGING_P bit set.
  1778.    In this case, also store 1 in hash_arg_in_struct
  1779.    if there is a MEM rtx which has the MEM_IN_STRUCT_P bit set.
  1780.  
  1781.    Note that cse_insn knows that the hash code of a MEM expression
  1782.    is just (int) MEM plus the hash code of the address.  */
  1783.  
  1784. static int
  1785. canon_hash (x, mode)
  1786.      rtx x;
  1787.      enum machine_mode mode;
  1788. {
  1789.   register int i, j;
  1790.   register int hash = 0;
  1791.   register enum rtx_code code;
  1792.   register char *fmt;
  1793.  
  1794.   /* repeat is used to turn tail-recursion into iteration.  */
  1795.  repeat:
  1796.   if (x == 0)
  1797.     return hash;
  1798.  
  1799.   code = GET_CODE (x);
  1800.   switch (code)
  1801.     {
  1802.     case REG:
  1803.       {
  1804.     register int regno = REGNO (x);
  1805.  
  1806.     /* On some machines, we can't record any non-fixed hard register,
  1807.        because extending its life will cause reload problems.  We
  1808.        consider ap, fp, and sp to be fixed for this purpose.
  1809.        On all machines, we can't record any global registers. */
  1810.  
  1811.     if (regno < FIRST_PSEUDO_REGISTER
  1812.         && (global_regs[regno]
  1813. #ifdef SMALL_REGISTER_CLASSES
  1814.         || (! fixed_regs[regno]
  1815.             && regno != FRAME_POINTER_REGNUM
  1816.             && regno != ARG_POINTER_REGNUM
  1817.             && regno != STACK_POINTER_REGNUM)
  1818. #endif
  1819.         ))
  1820.       {
  1821.         do_not_record = 1;
  1822.         return 0;
  1823.       }
  1824.     return hash + ((int) REG << 7) + reg_qty[regno];
  1825.       }
  1826.  
  1827.     case CONST_INT:
  1828.       hash += ((int) mode + ((int) CONST_INT << 7)
  1829.            + INTVAL (x) + (INTVAL (x) >> HASHBITS));
  1830.       return ((1 << HASHBITS) - 1) & hash;
  1831.  
  1832.     case CONST_DOUBLE:
  1833.       /* This is like the general case, except that it only counts
  1834.      the integers representing the constant.  */
  1835.       hash += (int) code + (int) GET_MODE (x);
  1836.       {
  1837.     int i;
  1838.     for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
  1839.       {
  1840.         int tem = XINT (x, i);
  1841.         hash += ((1 << HASHBITS) - 1) & (tem + (tem >> HASHBITS));
  1842.       }
  1843.       }
  1844.       return hash;
  1845.  
  1846.       /* Assume there is only one rtx object for any given label.  */
  1847.     case LABEL_REF:
  1848.       /* Use `and' to ensure a positive number.  */
  1849.       return (hash + ((HOST_WIDE_INT) LABEL_REF << 7)
  1850.           + ((HOST_WIDE_INT) XEXP (x, 0) & ((1 << HASHBITS) - 1)));
  1851.  
  1852.     case SYMBOL_REF:
  1853.       return (hash + ((HOST_WIDE_INT) SYMBOL_REF << 7)
  1854.           + ((HOST_WIDE_INT) XEXP (x, 0) & ((1 << HASHBITS) - 1)));
  1855.  
  1856.     case MEM:
  1857.       if (MEM_VOLATILE_P (x))
  1858.     {
  1859.       do_not_record = 1;
  1860.       return 0;
  1861.     }
  1862.       if (! RTX_UNCHANGING_P (x))
  1863.     {
  1864.       hash_arg_in_memory = 1;
  1865.       if (MEM_IN_STRUCT_P (x)) hash_arg_in_struct = 1;
  1866.     }
  1867.       /* Now that we have already found this special case,
  1868.      might as well speed it up as much as possible.  */
  1869.       hash += (int) MEM;
  1870.       x = XEXP (x, 0);
  1871.       goto repeat;
  1872.  
  1873.     case PRE_DEC:
  1874.     case PRE_INC:
  1875.     case POST_DEC:
  1876.     case POST_INC:
  1877.     case PC:
  1878.     case CC0:
  1879.     case CALL:
  1880.     case UNSPEC_VOLATILE:
  1881.       do_not_record = 1;
  1882.       return 0;
  1883.  
  1884.     case ASM_OPERANDS:
  1885.       if (MEM_VOLATILE_P (x))
  1886.     {
  1887.       do_not_record = 1;
  1888.       return 0;
  1889.     }
  1890.     }
  1891.  
  1892.   i = GET_RTX_LENGTH (code) - 1;
  1893.   hash += (int) code + (int) GET_MODE (x);
  1894.   fmt = GET_RTX_FORMAT (code);
  1895.   for (; i >= 0; i--)
  1896.     {
  1897.       if (fmt[i] == 'e')
  1898.     {
  1899.       rtx tem = XEXP (x, i);
  1900.       rtx tem1;
  1901.  
  1902.       /* If the operand is a REG that is equivalent to a constant, hash
  1903.          as if we were hashing the constant, since we will be comparing
  1904.          that way.  */
  1905.       if (tem != 0 && GET_CODE (tem) == REG
  1906.           && REGNO_QTY_VALID_P (REGNO (tem))
  1907.           && qty_mode[reg_qty[REGNO (tem)]] == GET_MODE (tem)
  1908.           && (tem1 = qty_const[reg_qty[REGNO (tem)]]) != 0
  1909.           && CONSTANT_P (tem1))
  1910.         tem = tem1;
  1911.  
  1912.       /* If we are about to do the last recursive call
  1913.          needed at this level, change it into iteration.
  1914.          This function  is called enough to be worth it.  */
  1915.       if (i == 0)
  1916.         {
  1917.           x = tem;
  1918.           goto repeat;
  1919.         }
  1920.       hash += canon_hash (tem, 0);
  1921.     }
  1922.       else if (fmt[i] == 'E')
  1923.     for (j = 0; j < XVECLEN (x, i); j++)
  1924.       hash += canon_hash (XVECEXP (x, i, j), 0);
  1925.       else if (fmt[i] == 's')
  1926.     {
  1927.       register char *p = XSTR (x, i);
  1928.       if (p)
  1929.         while (*p)
  1930.           {
  1931.         register int tem = *p++;
  1932.         hash += ((1 << HASHBITS) - 1) & (tem + (tem >> HASHBITS));
  1933.           }
  1934.     }
  1935.       else if (fmt[i] == 'i')
  1936.     {
  1937.       register int tem = XINT (x, i);
  1938.       hash += ((1 << HASHBITS) - 1) & (tem + (tem >> HASHBITS));
  1939.     }
  1940.       else
  1941. #ifdef APPLE_HAX
  1942.     {debug_rtx(x); fprintf (stderr, " %s\n", mode_name[(int) mode]);abort ();}
  1943. #else
  1944.     abort ();
  1945. #endif
  1946.     }
  1947.   return hash;
  1948. }
  1949.  
  1950. /* Like canon_hash but with no side effects.  */
  1951.  
  1952. static int
  1953. safe_hash (x, mode)
  1954.      rtx x;
  1955.      enum machine_mode mode;
  1956. {
  1957.   int save_do_not_record = do_not_record;
  1958.   int save_hash_arg_in_memory = hash_arg_in_memory;
  1959.   int save_hash_arg_in_struct = hash_arg_in_struct;
  1960.   int hash = canon_hash (x, mode);
  1961.   hash_arg_in_memory = save_hash_arg_in_memory;
  1962.   hash_arg_in_struct = save_hash_arg_in_struct;
  1963.   do_not_record = save_do_not_record;
  1964.   return hash;
  1965. }
  1966.  
  1967. /* Return 1 iff X and Y would canonicalize into the same thing,
  1968.    without actually constructing the canonicalization of either one.
  1969.    If VALIDATE is nonzero,
  1970.    we assume X is an expression being processed from the rtl
  1971.    and Y was found in the hash table.  We check register refs
  1972.    in Y for being marked as valid.
  1973.  
  1974.    If EQUAL_VALUES is nonzero, we allow a register to match a constant value
  1975.    that is known to be in the register.  Ordinarily, we don't allow them
  1976.    to match, because letting them match would cause unpredictable results
  1977.    in all the places that search a hash table chain for an equivalent
  1978.    for a given value.  A possible equivalent that has different structure
  1979.    has its hash code computed from different data.  Whether the hash code
  1980.    is the same as that of the the given value is pure luck.  */
  1981.  
  1982. static int
  1983. exp_equiv_p (x, y, validate, equal_values)
  1984.      rtx x, y;
  1985.      int validate;
  1986.      int equal_values;
  1987. {
  1988.   register int i, j;
  1989.   register enum rtx_code code;
  1990.   register char *fmt;
  1991.  
  1992.   /* Note: it is incorrect to assume an expression is equivalent to itself
  1993.      if VALIDATE is nonzero.  */
  1994.   if (x == y && !validate)
  1995.     return 1;
  1996.   if (x == 0 || y == 0)
  1997.     return x == y;
  1998.  
  1999.   code = GET_CODE (x);
  2000.   if (code != GET_CODE (y))
  2001.     {
  2002.       if (!equal_values)
  2003.     return 0;
  2004.  
  2005.       /* If X is a constant and Y is a register or vice versa, they may be
  2006.      equivalent.  We only have to validate if Y is a register.  */
  2007.       if (CONSTANT_P (x) && GET_CODE (y) == REG
  2008.       && REGNO_QTY_VALID_P (REGNO (y))
  2009.       && GET_MODE (y) == qty_mode[reg_qty[REGNO (y)]]
  2010.       && rtx_equal_p (x, qty_const[reg_qty[REGNO (y)]])
  2011.       && (! validate || reg_in_table[REGNO (y)] == reg_tick[REGNO (y)]))
  2012.     return 1;
  2013.  
  2014.       if (CONSTANT_P (y) && code == REG
  2015.       && REGNO_QTY_VALID_P (REGNO (x))
  2016.       && GET_MODE (x) == qty_mode[reg_qty[REGNO (x)]]
  2017.       && rtx_equal_p (y, qty_const[reg_qty[REGNO (x)]]))
  2018.     return 1;
  2019.  
  2020.       return 0;
  2021.     }
  2022.  
  2023.   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
  2024.   if (GET_MODE (x) != GET_MODE (y))
  2025.     return 0;
  2026.  
  2027.   switch (code)
  2028.     {
  2029.     case PC:
  2030.     case CC0:
  2031.       return x == y;
  2032.  
  2033.     case CONST_INT:
  2034.       return INTVAL (x) == INTVAL (y);
  2035.  
  2036.     case LABEL_REF:
  2037.     case SYMBOL_REF:
  2038.       return XEXP (x, 0) == XEXP (y, 0);
  2039.  
  2040.     case REG:
  2041.       {
  2042.     int regno = REGNO (y);
  2043.     int endregno
  2044.       = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1
  2045.              : HARD_REGNO_NREGS (regno, GET_MODE (y)));
  2046.     int i;
  2047.  
  2048.     /* If the quantities are not the same, the expressions are not
  2049.        equivalent.  If there are and we are not to validate, they
  2050.        are equivalent.  Otherwise, ensure all regs are up-to-date.  */
  2051.  
  2052.     if (reg_qty[REGNO (x)] != reg_qty[regno])
  2053.       return 0;
  2054.  
  2055.     if (! validate)
  2056.       return 1;
  2057.  
  2058.     for (i = regno; i < endregno; i++)
  2059.       if (reg_in_table[i] != reg_tick[i])
  2060.         return 0;
  2061.  
  2062.     return 1;
  2063.       }
  2064.  
  2065.     /*  For commutative operations, check both orders.  */
  2066.     case PLUS:
  2067.     case MULT:
  2068.     case AND:
  2069.     case IOR:
  2070.     case XOR:
  2071.     case NE:
  2072.     case EQ:
  2073.       return ((exp_equiv_p (XEXP (x, 0), XEXP (y, 0), validate, equal_values)
  2074.            && exp_equiv_p (XEXP (x, 1), XEXP (y, 1),
  2075.                    validate, equal_values))
  2076.           || (exp_equiv_p (XEXP (x, 0), XEXP (y, 1),
  2077.                    validate, equal_values)
  2078.           && exp_equiv_p (XEXP (x, 1), XEXP (y, 0),
  2079.                   validate, equal_values)));
  2080.     }
  2081.  
  2082.   /* Compare the elements.  If any pair of corresponding elements
  2083.      fail to match, return 0 for the whole things.  */
  2084.  
  2085.   fmt = GET_RTX_FORMAT (code);
  2086.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2087.     {
  2088.       switch (fmt[i])
  2089.     {
  2090.     case 'e':
  2091.       if (! exp_equiv_p (XEXP (x, i), XEXP (y, i), validate, equal_values))
  2092.         return 0;
  2093.       break;
  2094.  
  2095.     case 'E':
  2096.       if (XVECLEN (x, i) != XVECLEN (y, i))
  2097.         return 0;
  2098.       for (j = 0; j < XVECLEN (x, i); j++)
  2099.         if (! exp_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
  2100.                    validate, equal_values))
  2101.           return 0;
  2102.       break;
  2103.  
  2104.     case 's':
  2105.       if (strcmp (XSTR (x, i), XSTR (y, i)))
  2106.         return 0;
  2107.       break;
  2108.  
  2109.     case 'i':
  2110.       if (XINT (x, i) != XINT (y, i))
  2111.         return 0;
  2112.       break;
  2113.  
  2114.     case 'w':
  2115.       if (XWINT (x, i) != XWINT (y, i))
  2116.         return 0;
  2117.     break;
  2118.  
  2119.     case '0':
  2120.       break;
  2121.  
  2122.     default:
  2123.       abort ();
  2124.     }
  2125.       }
  2126.  
  2127.   return 1;
  2128. }
  2129.  
  2130. /* Return 1 iff any subexpression of X matches Y.
  2131.    Here we do not require that X or Y be valid (for registers referred to)
  2132.    for being in the hash table.  */
  2133.  
  2134. int
  2135. refers_to_p (x, y)
  2136.      rtx x, y;
  2137. {
  2138.   register int i;
  2139.   register enum rtx_code code;
  2140.   register char *fmt;
  2141.  
  2142.  repeat:
  2143.   if (x == y)
  2144.     return 1;
  2145.   if (x == 0 || y == 0)
  2146.     return 0;
  2147.  
  2148.   code = GET_CODE (x);
  2149.   /* If X as a whole has the same code as Y, they may match.
  2150.      If so, return 1.  */
  2151.   if (code == GET_CODE (y))
  2152.     {
  2153.       if (exp_equiv_p (x, y, 0, 1))
  2154.     return 1;
  2155.     }
  2156.  
  2157.   /* X does not match, so try its subexpressions.  */
  2158.  
  2159.   fmt = GET_RTX_FORMAT (code);
  2160.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2161.     if (fmt[i] == 'e')
  2162.       {
  2163.     if (i == 0)
  2164.       {
  2165.         x = XEXP (x, 0);
  2166.         goto repeat;
  2167.       }
  2168.     else
  2169.       if (refers_to_p (XEXP (x, i), y))
  2170.         return 1;
  2171.       }
  2172.     else if (fmt[i] == 'E')
  2173.       {
  2174.     int j;
  2175.     for (j = 0; j < XVECLEN (x, i); j++)
  2176.       if (refers_to_p (XVECEXP (x, i, j), y))
  2177.         return 1;
  2178.       }
  2179.  
  2180.   return 0;
  2181. }
  2182.  
  2183. /* Return 1 iff any subexpression of X refers to memory
  2184.    at an address of BASE plus some offset
  2185.    such that any of the bytes' offsets fall between START (inclusive)
  2186.    and END (exclusive).
  2187.  
  2188.    The value is undefined if X is a varying address.
  2189.    This function is not used in such cases.
  2190.  
  2191.    When used in the cse pass, `qty_const' is nonzero, and it is used
  2192.    to treat an address that is a register with a known constant value
  2193.    as if it were that constant value.
  2194.    In the loop pass, `qty_const' is zero, so this is not done.  */
  2195.  
  2196. int
  2197. refers_to_mem_p (x, base, start, end)
  2198.      rtx x, base;
  2199.      HOST_WIDE_INT start, end;
  2200. {
  2201.   register HOST_WIDE_INT i;
  2202.   register enum rtx_code code;
  2203.   register char *fmt;
  2204.  
  2205.   if (GET_CODE (base) == CONST_INT)
  2206.     {
  2207.       start += INTVAL (base);
  2208.       end += INTVAL (base);
  2209.       base = const0_rtx;
  2210.     }
  2211.  
  2212.  repeat:
  2213.   if (x == 0)
  2214.     return 0;
  2215.  
  2216.   code = GET_CODE (x);
  2217.   if (code == MEM)
  2218.     {
  2219.       register rtx addr = XEXP (x, 0);    /* Get the address.  */
  2220.       int myend;
  2221.  
  2222.       i = 0;
  2223.       if (GET_CODE (addr) == REG
  2224.       /* qty_const is 0 when outside the cse pass;
  2225.          at such times, this info is not available.  */
  2226.       && qty_const != 0
  2227.       && REGNO_QTY_VALID_P (REGNO (addr))
  2228.       && GET_MODE (addr) == qty_mode[reg_qty[REGNO (addr)]]
  2229.       && qty_const[reg_qty[REGNO (addr)]] != 0)
  2230.     addr = qty_const[reg_qty[REGNO (addr)]];
  2231.       else if (GET_CODE (addr) == PLUS
  2232.            && GET_CODE (XEXP (addr, 1)) == CONST_INT
  2233.            && GET_CODE (XEXP (addr, 0)) == REG
  2234.            && qty_const != 0
  2235.            && REGNO_QTY_VALID_P (REGNO (XEXP (addr, 0)))
  2236.            && (GET_MODE (XEXP (addr, 0))
  2237.            == qty_mode[reg_qty[REGNO (XEXP (addr, 0))]])
  2238.            && qty_const[reg_qty[REGNO (XEXP (addr, 0))]])
  2239.     {
  2240.       i = INTVAL (XEXP (addr, 1));
  2241.       addr = qty_const[reg_qty[REGNO (XEXP (addr, 0))]];
  2242.     }
  2243.  
  2244.     check_addr:
  2245.       if (GET_CODE (addr) == CONST)
  2246.     addr = XEXP (addr, 0);
  2247.  
  2248.       /* If ADDR is BASE, or BASE plus an integer, put
  2249.      the integer in I.  */
  2250.       if (GET_CODE (addr) == PLUS
  2251.       && XEXP (addr, 0) == base
  2252.       && GET_CODE (XEXP (addr, 1)) == CONST_INT)
  2253.     i += INTVAL (XEXP (addr, 1));
  2254.       else if (GET_CODE (addr) == LO_SUM)
  2255.     {
  2256.       if (GET_CODE (base) != LO_SUM)
  2257.         return 1;
  2258.       /* The REG component of the LO_SUM is known by the
  2259.          const value in the XEXP part.  */
  2260.       addr = XEXP (addr, 1);
  2261.       base = XEXP (base, 1);
  2262.       i = 0;
  2263.       if (GET_CODE (base) == CONST)
  2264.         base = XEXP (base, 0);
  2265.       if (GET_CODE (base) == PLUS
  2266.           && GET_CODE (XEXP (base, 1)) == CONST_INT)
  2267.         {
  2268.           HOST_WIDE_INT tem = INTVAL (XEXP (base, 1));
  2269.           start += tem;
  2270.           end += tem;
  2271.           base = XEXP (base, 0);
  2272.         }
  2273.       goto check_addr;
  2274.     }
  2275.       else if (GET_CODE (base) == LO_SUM)
  2276.     {
  2277.       base = XEXP (base, 1);
  2278.       if (GET_CODE (base) == CONST)
  2279.         base = XEXP (base, 0);
  2280.       if (GET_CODE (base) == PLUS
  2281.           && GET_CODE (XEXP (base, 1)) == CONST_INT)
  2282.         {
  2283.           HOST_WIDE_INT tem = INTVAL (XEXP (base, 1));
  2284.           start += tem;
  2285.           end += tem;
  2286.           base = XEXP (base, 0);
  2287.         }
  2288.       goto check_addr;      
  2289.     }
  2290.       else if (GET_CODE (addr) == CONST_INT && base == const0_rtx)
  2291.     i = INTVAL (addr);
  2292.       else if (addr != base)
  2293.     return 0;
  2294.  
  2295.       myend = i + GET_MODE_SIZE (GET_MODE (x));
  2296.       return myend > start && i < end;
  2297.     }
  2298.  
  2299.   /* X does not match, so try its subexpressions.  */
  2300.  
  2301.   fmt = GET_RTX_FORMAT (code);
  2302.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2303.     if (fmt[i] == 'e')
  2304.       {
  2305.     if (i == 0)
  2306.       {
  2307.         x = XEXP (x, 0);
  2308.         goto repeat;
  2309.       }
  2310.     else
  2311.       if (refers_to_mem_p (XEXP (x, i), base, start, end))
  2312.         return 1;
  2313.       }
  2314.     else if (fmt[i] == 'E')
  2315.       {
  2316.     int j;
  2317.     for (j = 0; j < XVECLEN (x, i); j++)
  2318.       if (refers_to_mem_p (XVECEXP (x, i, j), base, start, end))
  2319.         return 1;
  2320.       }
  2321.  
  2322.   return 0;
  2323. }
  2324.  
  2325. /* Nonzero if X refers to memory at a varying address;
  2326.    except that a register which has at the moment a known constant value
  2327.    isn't considered variable.  */
  2328.  
  2329. static int
  2330. cse_rtx_addr_varies_p (x)
  2331.      rtx x;
  2332. {
  2333.   /* We need not check for X and the equivalence class being of the same
  2334.      mode because if X is equivalent to a constant in some mode, it
  2335.      doesn't vary in any mode.  */
  2336.  
  2337.   if (GET_CODE (x) == MEM
  2338.       && GET_CODE (XEXP (x, 0)) == REG
  2339.       && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0)))
  2340.       && GET_MODE (XEXP (x, 0)) == qty_mode[reg_qty[REGNO (XEXP (x, 0))]]
  2341.       && qty_const[reg_qty[REGNO (XEXP (x, 0))]] != 0)
  2342.     return 0;
  2343.  
  2344.   if (GET_CODE (x) == MEM
  2345.       && GET_CODE (XEXP (x, 0)) == PLUS
  2346.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  2347.       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
  2348.       && REGNO_QTY_VALID_P (REGNO (XEXP (XEXP (x, 0), 0)))
  2349.       && (GET_MODE (XEXP (XEXP (x, 0), 0))
  2350.       == qty_mode[reg_qty[REGNO (XEXP (XEXP (x, 0), 0))]])
  2351.       && qty_const[reg_qty[REGNO (XEXP (XEXP (x, 0), 0))]])
  2352.     return 0;
  2353.  
  2354.   return rtx_addr_varies_p (x);
  2355. }
  2356.  
  2357. /* Canonicalize an expression:
  2358.    replace each register reference inside it
  2359.    with the "oldest" equivalent register.
  2360.  
  2361.    If INSN is non-zero and we are replacing a pseudo with a hard register
  2362.    or vice versa, validate_change is used to ensure that INSN remains valid
  2363.    after we make our substitution.  The calls are made with IN_GROUP non-zero
  2364.    so apply_change_group must be called upon the outermost return from this
  2365.    function (unless INSN is zero).  The result of apply_change_group can
  2366.    generally be discarded since the changes we are making are optional.  */
  2367.  
  2368. static rtx
  2369. canon_reg (x, insn)
  2370.      rtx x;
  2371.      rtx insn;
  2372. {
  2373.   register int i;
  2374.   register enum rtx_code code;
  2375.   register char *fmt;
  2376.  
  2377.   if (x == 0)
  2378.     return x;
  2379.  
  2380.   code = GET_CODE (x);
  2381.   switch (code)
  2382.     {
  2383.     case PC:
  2384.     case CC0:
  2385.     case CONST:
  2386.     case CONST_INT:
  2387.     case CONST_DOUBLE:
  2388.     case SYMBOL_REF:
  2389.     case LABEL_REF:
  2390.     case ADDR_VEC:
  2391.     case ADDR_DIFF_VEC:
  2392.       return x;
  2393.  
  2394.     case REG:
  2395.       {
  2396.     register int first;
  2397.  
  2398.     /* Never replace a hard reg, because hard regs can appear
  2399.        in more than one machine mode, and we must preserve the mode
  2400.        of each occurrence.  Also, some hard regs appear in
  2401.        MEMs that are shared and mustn't be altered.  Don't try to
  2402.        replace any reg that maps to a reg of class NO_REGS.  */
  2403.     if (REGNO (x) < FIRST_PSEUDO_REGISTER
  2404.         || ! REGNO_QTY_VALID_P (REGNO (x)))
  2405.       return x;
  2406.  
  2407.     first = qty_first_reg[reg_qty[REGNO (x)]];
  2408.     return (first >= FIRST_PSEUDO_REGISTER ? regno_reg_rtx[first]
  2409.         : REGNO_REG_CLASS (first) == NO_REGS ? x
  2410.         : gen_rtx (REG, qty_mode[reg_qty[REGNO (x)]], first));
  2411.       }
  2412.     }
  2413.  
  2414.   fmt = GET_RTX_FORMAT (code);
  2415.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2416.     {
  2417.       register int j;
  2418.  
  2419.       if (fmt[i] == 'e')
  2420.     {
  2421.       rtx new = canon_reg (XEXP (x, i), insn);
  2422.  
  2423.       /* If replacing pseudo with hard reg or vice versa, ensure the
  2424.          insn remains valid.  Likewise if the insn has MATCH_DUPs.  */
  2425.       if (insn != 0 && new != 0
  2426.           && GET_CODE (new) == REG && GET_CODE (XEXP (x, i)) == REG
  2427.           && (((REGNO (new) < FIRST_PSEUDO_REGISTER)
  2428.            != (REGNO (XEXP (x, i)) < FIRST_PSEUDO_REGISTER))
  2429.           || insn_n_dups[recog_memoized (insn)] > 0))
  2430.         validate_change (insn, &XEXP (x, i), new, 1);
  2431.       else
  2432.         XEXP (x, i) = new;
  2433.     }
  2434.       else if (fmt[i] == 'E')
  2435.     for (j = 0; j < XVECLEN (x, i); j++)
  2436.       XVECEXP (x, i, j) = canon_reg (XVECEXP (x, i, j), insn);
  2437.     }
  2438.  
  2439.   return x;
  2440. }
  2441.  
  2442. /* LOC is a location with INSN that is an operand address (the contents of
  2443.    a MEM).  Find the best equivalent address to use that is valid for this
  2444.    insn.
  2445.  
  2446.    On most CISC machines, complicated address modes are costly, and rtx_cost
  2447.    is a good approximation for that cost.  However, most RISC machines have
  2448.    only a few (usually only one) memory reference formats.  If an address is
  2449.    valid at all, it is often just as cheap as any other address.  Hence, for
  2450.    RISC machines, we use the configuration macro `ADDRESS_COST' to compare the
  2451.    costs of various addresses.  For two addresses of equal cost, choose the one
  2452.    with the highest `rtx_cost' value as that has the potential of eliminating
  2453.    the most insns.  For equal costs, we choose the first in the equivalence
  2454.    class.  Note that we ignore the fact that pseudo registers are cheaper
  2455.    than hard registers here because we would also prefer the pseudo registers.
  2456.   */
  2457.  
  2458. void
  2459. find_best_addr (insn, loc)
  2460.      rtx insn;
  2461.      rtx *loc;
  2462. {
  2463.   struct table_elt *elt, *p;
  2464.   rtx addr = *loc;
  2465.   int our_cost;
  2466.   int found_better = 1;
  2467.   int save_do_not_record = do_not_record;
  2468.   int save_hash_arg_in_memory = hash_arg_in_memory;
  2469.   int save_hash_arg_in_struct = hash_arg_in_struct;
  2470.   int hash_code;
  2471.   int addr_volatile;
  2472.   int regno;
  2473.  
  2474.   /* Do not try to replace constant addresses or addresses of local and
  2475.      argument slots.  These MEM expressions are made only once and inserted
  2476.      in many instructions, as well as being used to control symbol table
  2477.      output.  It is not safe to clobber them.
  2478.  
  2479.      There are some uncommon cases where the address is already in a register
  2480.      for some reason, but we cannot take advantage of that because we have
  2481.      no easy way to unshare the MEM.  In addition, looking up all stack
  2482.      addresses is costly.  */
  2483.   if ((GET_CODE (addr) == PLUS
  2484.        && GET_CODE (XEXP (addr, 0)) == REG
  2485.        && GET_CODE (XEXP (addr, 1)) == CONST_INT
  2486.        && (regno = REGNO (XEXP (addr, 0)),
  2487.        regno == FRAME_POINTER_REGNUM || regno == ARG_POINTER_REGNUM))
  2488.       || (GET_CODE (addr) == REG
  2489.       && (regno = REGNO (addr),
  2490.           regno == FRAME_POINTER_REGNUM || regno == ARG_POINTER_REGNUM))
  2491.       || CONSTANT_ADDRESS_P (addr))
  2492.     return;
  2493.  
  2494.   /* If this address is not simply a register, try to fold it.  This will
  2495.      sometimes simplify the expression.  Many simplifications
  2496.      will not be valid, but some, usually applying the associative rule, will
  2497.      be valid and produce better code.  */
  2498.   if (GET_CODE (addr) != REG
  2499.       && validate_change (insn, loc, fold_rtx (addr, insn), 0))
  2500.     addr = *loc;
  2501.     
  2502.   /* If this address is not in the hash table, we can't look for equivalences
  2503.      of the whole address.  Also, ignore if volatile.  */
  2504.  
  2505.   do_not_record = 0;
  2506.   hash_code = HASH (addr, DPmode);
  2507.   addr_volatile = do_not_record;
  2508.   do_not_record = save_do_not_record;
  2509.   hash_arg_in_memory = save_hash_arg_in_memory;
  2510.   hash_arg_in_struct = save_hash_arg_in_struct;
  2511.  
  2512.   if (addr_volatile)
  2513.     return;
  2514.  
  2515.   elt = lookup (addr, hash_code, DPmode);
  2516.  
  2517. #ifndef ADDRESS_COST
  2518.   if (elt)
  2519.     {
  2520.       our_cost = elt->cost;
  2521.  
  2522.       /* Find the lowest cost below ours that works.  */
  2523.       for (elt = elt->first_same_value; elt; elt = elt->next_same_value)
  2524.     if (elt->cost < our_cost
  2525.         && (GET_CODE (elt->exp) == REG
  2526.         || exp_equiv_p (elt->exp, elt->exp, 1, 0))
  2527.         && validate_change (insn, loc,
  2528.                 canon_reg (copy_rtx (elt->exp), NULL_RTX), 0))
  2529.       return;
  2530.     }
  2531. #else
  2532.  
  2533.   if (elt)
  2534.     {
  2535.       /* We need to find the best (under the criteria documented above) entry
  2536.      in the class that is valid.  We use the `flag' field to indicate
  2537.      choices that were invalid and iterate until we can't find a better
  2538.      one that hasn't already been tried.  */
  2539.  
  2540.       for (p = elt->first_same_value; p; p = p->next_same_value)
  2541.     p->flag = 0;
  2542.  
  2543.       while (found_better)
  2544.     {
  2545.       int best_addr_cost = ADDRESS_COST (*loc);
  2546.       int best_rtx_cost = (elt->cost + 1) >> 1;
  2547.       struct table_elt *best_elt = elt; 
  2548.  
  2549.       found_better = 0;
  2550.       for (p = elt->first_same_value; p; p = p->next_same_value)
  2551.         if (! p->flag
  2552.         && (GET_CODE (p->exp) == REG
  2553.             || exp_equiv_p (p->exp, p->exp, 1, 0))
  2554.         && (ADDRESS_COST (p->exp) < best_addr_cost
  2555.             || (ADDRESS_COST (p->exp) == best_addr_cost
  2556.             && (p->cost + 1) >> 1 > best_rtx_cost)))
  2557.           {
  2558.         found_better = 1;
  2559.         best_addr_cost = ADDRESS_COST (p->exp);
  2560.         best_rtx_cost = (p->cost + 1) >> 1;
  2561.         best_elt = p;
  2562.           }
  2563.  
  2564.       if (found_better)
  2565.         {
  2566.           if (validate_change (insn, loc,
  2567.                    canon_reg (copy_rtx (best_elt->exp),
  2568.                           NULL_RTX), 0))
  2569.         return;
  2570.           else
  2571.         best_elt->flag = 1;
  2572.         }
  2573.     }
  2574.     }
  2575.  
  2576.   /* If the address is a binary operation with the first operand a register
  2577.      and the second a constant, do the same as above, but looking for
  2578.      equivalences of the register.  Then try to simplify before checking for
  2579.      the best address to use.  This catches a few cases:  First is when we
  2580.      have REG+const and the register is another REG+const.  We can often merge
  2581.      the constants and eliminate one insn and one register.  It may also be
  2582.      that a machine has a cheap REG+REG+const.  Finally, this improves the
  2583.      code on the Alpha for unaligned byte stores.  */
  2584.  
  2585.   if (flag_expensive_optimizations
  2586.       && (GET_RTX_CLASS (GET_CODE (*loc)) == '2'
  2587.       || GET_RTX_CLASS (GET_CODE (*loc)) == 'c')
  2588.       && GET_CODE (XEXP (*loc, 0)) == REG
  2589.       && GET_CODE (XEXP (*loc, 1)) == CONST_INT)
  2590.     {
  2591.       rtx c = XEXP (*loc, 1);
  2592.  
  2593.       do_not_record = 0;
  2594.       hash_code = HASH (XEXP (*loc, 0), DPmode);
  2595.       do_not_record = save_do_not_record;
  2596.       hash_arg_in_memory = save_hash_arg_in_memory;
  2597.       hash_arg_in_struct = save_hash_arg_in_struct;
  2598.  
  2599.       elt = lookup (XEXP (*loc, 0), hash_code, DPmode);
  2600.       if (elt == 0)
  2601.     return;
  2602.  
  2603.       /* We need to find the best (under the criteria documented above) entry
  2604.      in the class that is valid.  We use the `flag' field to indicate
  2605.      choices that were invalid and iterate until we can't find a better
  2606.      one that hasn't already been tried.  */
  2607.  
  2608.       for (p = elt->first_same_value; p; p = p->next_same_value)
  2609.     p->flag = 0;
  2610.  
  2611.       while (found_better)
  2612.     {
  2613.       int best_addr_cost = ADDRESS_COST (*loc);
  2614.       int best_rtx_cost = (COST (*loc) + 1) >> 1;
  2615.       struct table_elt *best_elt = elt; 
  2616.       rtx best_rtx = *loc;
  2617.  
  2618.       found_better = 0;
  2619.       for (p = elt->first_same_value; p; p = p->next_same_value)
  2620.         if (! p->flag
  2621.         && (GET_CODE (p->exp) == REG
  2622.             || exp_equiv_p (p->exp, p->exp, 1, 0)))
  2623.           {
  2624.         rtx new = simplify_binary_operation (GET_CODE (*loc), DPmode,
  2625.                              p->exp, c);
  2626.  
  2627.         if (new == 0)
  2628.           new = gen_rtx (GET_CODE (*loc), DPmode, p->exp, c);
  2629.  
  2630.         if ((ADDRESS_COST (new) < best_addr_cost
  2631.             || (ADDRESS_COST (new) == best_addr_cost
  2632.             && (COST (new) + 1) >> 1 > best_rtx_cost)))
  2633.           {
  2634.             found_better = 1;
  2635.             best_addr_cost = ADDRESS_COST (new);
  2636.             best_rtx_cost = (COST (new) + 1) >> 1;
  2637.             best_elt = p;
  2638.             best_rtx = new;
  2639.           }
  2640.           }
  2641.  
  2642.       if (found_better)
  2643.         {
  2644.           if (validate_change (insn, loc,
  2645.                    canon_reg (copy_rtx (best_rtx),
  2646.                           NULL_RTX), 0))
  2647.         return;
  2648.           else
  2649.         best_elt->flag = 1;
  2650.         }
  2651.     }
  2652.     }
  2653. #endif
  2654. }
  2655.  
  2656. /* Given an operation (CODE, *PARG1, *PARG2), where code is a comparison
  2657.    operation (EQ, NE, GT, etc.), follow it back through the hash table and
  2658.    what values are being compared.
  2659.  
  2660.    *PARG1 and *PARG2 are updated to contain the rtx representing the values
  2661.    actually being compared.  For example, if *PARG1 was (cc0) and *PARG2
  2662.    was (const_int 0), *PARG1 and *PARG2 will be set to the objects that were
  2663.    compared to produce cc0.
  2664.  
  2665.    The return value is the comparison operator and is either the code of
  2666.    A or the code corresponding to the inverse of the comparison.  */
  2667.  
  2668. static enum rtx_code
  2669. find_comparison_args (code, parg1, parg2, pmode1, pmode2)
  2670.      enum rtx_code code;
  2671.      rtx *parg1, *parg2;
  2672.      enum machine_mode *pmode1, *pmode2;
  2673. {
  2674.   rtx arg1, arg2;
  2675.  
  2676.   arg1 = *parg1, arg2 = *parg2;
  2677.  
  2678.   /* If ARG2 is const0_rtx, see what ARG1 is equivalent to.  */
  2679.  
  2680.   while (arg2 == CONST0_RTX (GET_MODE (arg1)))
  2681.     {
  2682.       /* Set non-zero when we find something of interest.  */
  2683.       rtx x = 0;
  2684.       int reverse_code = 0;
  2685.       struct table_elt *p = 0;
  2686.  
  2687.       /* If arg1 is a COMPARE, extract the comparison arguments from it.
  2688.      On machines with CC0, this is the only case that can occur, since
  2689.      fold_rtx will return the COMPARE or item being compared with zero
  2690.      when given CC0.  */
  2691.  
  2692.       if (GET_CODE (arg1) == COMPARE && arg2 == const0_rtx)
  2693.     x = arg1;
  2694.  
  2695.       /* If ARG1 is a comparison operator and CODE is testing for
  2696.      STORE_FLAG_VALUE, get the inner arguments.  */
  2697.  
  2698.       else if (GET_RTX_CLASS (GET_CODE (arg1)) == '<')
  2699.     {
  2700.       if (code == NE
  2701.           || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
  2702.           && code == LT && STORE_FLAG_VALUE == -1)
  2703. #ifdef FLOAT_STORE_FLAG_VALUE
  2704.           || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_FLOAT
  2705.           && FLOAT_STORE_FLAG_VALUE < 0)
  2706. #endif
  2707.           )
  2708.         x = arg1;
  2709.       else if (code == EQ
  2710.            || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
  2711.                && code == GE && STORE_FLAG_VALUE == -1)
  2712. #ifdef FLOAT_STORE_FLAG_VALUE
  2713.            || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_FLOAT
  2714.                && FLOAT_STORE_FLAG_VALUE < 0)
  2715. #endif
  2716.            )
  2717.         x = arg1, reverse_code = 1;
  2718.     }
  2719.  
  2720.       /* ??? We could also check for
  2721.  
  2722.      (ne (and (eq (...) (const_int 1))) (const_int 0))
  2723.  
  2724.      and related forms, but let's wait until we see them occurring.  */
  2725.  
  2726.       if (x == 0)
  2727.     /* Look up ARG1 in the hash table and see if it has an equivalence
  2728.        that lets us see what is being compared.  */
  2729.     p = lookup (arg1, safe_hash (arg1, GET_MODE (arg1)) % NBUCKETS,
  2730.             GET_MODE (arg1));
  2731.       if (p) p = p->first_same_value;
  2732.  
  2733.       for (; p; p = p->next_same_value)
  2734.     {
  2735.       enum machine_mode inner_mode = GET_MODE (p->exp);
  2736.  
  2737.       /* If the entry isn't valid, skip it.  */
  2738.       if (! exp_equiv_p (p->exp, p->exp, 1, 0))
  2739.         continue;
  2740.  
  2741.       if (GET_CODE (p->exp) == COMPARE
  2742.           /* Another possibility is that this machine has a compare insn
  2743.          that includes the comparison code.  In that case, ARG1 would
  2744.          be equivalent to a comparison operation that would set ARG1 to
  2745.          either STORE_FLAG_VALUE or zero.  If this is an NE operation,
  2746.          ORIG_CODE is the actual comparison being done; if it is an EQ,
  2747.          we must reverse ORIG_CODE.  On machine with a negative value
  2748.          for STORE_FLAG_VALUE, also look at LT and GE operations.  */
  2749.           || ((code == NE
  2750.            || (code == LT
  2751.                && GET_MODE_CLASS (inner_mode) == MODE_INT
  2752.                && (GET_MODE_BITSIZE (inner_mode)
  2753.                <= HOST_BITS_PER_WIDE_INT)
  2754.                && (STORE_FLAG_VALUE
  2755.                & ((HOST_WIDE_INT) 1
  2756.                   << (GET_MODE_BITSIZE (inner_mode) - 1))))
  2757. #ifdef FLOAT_STORE_FLAG_VALUE
  2758.            || (code == LT
  2759.                && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
  2760.                && FLOAT_STORE_FLAG_VALUE < 0)
  2761. #endif
  2762.            )
  2763.           && GET_RTX_CLASS (GET_CODE (p->exp)) == '<'))
  2764.         {
  2765.           x = p->exp;
  2766.           break;
  2767.         }
  2768.       else if ((code == EQ
  2769.             || (code == GE
  2770.             && GET_MODE_CLASS (inner_mode) == MODE_INT
  2771.             && (GET_MODE_BITSIZE (inner_mode)
  2772.                 <= HOST_BITS_PER_WIDE_INT)
  2773.             && (STORE_FLAG_VALUE
  2774.                 & ((HOST_WIDE_INT) 1
  2775.                    << (GET_MODE_BITSIZE (inner_mode) - 1))))
  2776. #ifdef FLOAT_STORE_FLAG_VALUE
  2777.             || (code == GE
  2778.             && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
  2779.             && FLOAT_STORE_FLAG_VALUE < 0)
  2780. #endif
  2781.             )
  2782.            && GET_RTX_CLASS (GET_CODE (p->exp)) == '<')
  2783.         {
  2784.           reverse_code = 1;
  2785.           x = p->exp;
  2786.           break;
  2787.         }
  2788.  
  2789.       /* If this is fp + constant, the equivalent is a better operand since
  2790.          it may let us predict the value of the comparison.  */
  2791.       else if (NONZERO_BASE_PLUS_P (p->exp))
  2792.         {
  2793.           arg1 = p->exp;
  2794.           continue;
  2795.         }
  2796.     }
  2797.  
  2798.       /* If we didn't find a useful equivalence for ARG1, we are done.
  2799.      Otherwise, set up for the next iteration.  */
  2800.       if (x == 0)
  2801.     break;
  2802.  
  2803.       arg1 = XEXP (x, 0),  arg2 = XEXP (x, 1);
  2804.       if (GET_RTX_CLASS (GET_CODE (x)) == '<')
  2805.     code = GET_CODE (x);
  2806.  
  2807.       if (reverse_code)
  2808.     code = reverse_condition (code);
  2809.     }
  2810.  
  2811.   /* Return our results.  Return the modes from before fold_rtx
  2812.      because fold_rtx might produce const_int, and then it's too late.  */
  2813.   *pmode1 = GET_MODE (arg1), *pmode2 = GET_MODE (arg2);
  2814.   *parg1 = fold_rtx (arg1, 0), *parg2 = fold_rtx (arg2, 0);
  2815.  
  2816.   return code;
  2817. }
  2818.  
  2819. /* Try to simplify a unary operation CODE whose output mode is to be
  2820.    MODE with input operand OP whose mode was originally OP_MODE.
  2821.    Return zero if no simplification can be made.  */
  2822.  
  2823. rtx
  2824. simplify_unary_operation (code, mode, op, op_mode)
  2825.      enum rtx_code code;
  2826.      enum machine_mode mode;
  2827.      rtx op;
  2828.      enum machine_mode op_mode;
  2829. {
  2830.   register int width = GET_MODE_BITSIZE (mode);
  2831.  
  2832.   /* The order of these tests is critical so that, for example, we don't
  2833.      check the wrong mode (input vs. output) for a conversion operation,
  2834.      such as FIX.  At some point, this should be simplified.  */
  2835.  
  2836. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  2837.   if (code == FLOAT && GET_CODE (op) == CONST_INT)
  2838.     {
  2839.       REAL_VALUE_TYPE d;
  2840.  
  2841. #ifdef REAL_ARITHMETIC
  2842.       REAL_VALUE_FROM_INT (d, INTVAL (op), INTVAL (op) < 0 ? ~0 : 0);
  2843. #else
  2844.       d = (double) INTVAL (op);
  2845. #endif
  2846.       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
  2847.     }
  2848.   else if (code == UNSIGNED_FLOAT && GET_CODE (op) == CONST_INT)
  2849.     {
  2850.       REAL_VALUE_TYPE d;
  2851.  
  2852. #ifdef REAL_ARITHMETIC
  2853.       REAL_VALUE_FROM_INT (d, INTVAL (op), 0);
  2854. #else
  2855.       d = (double) (unsigned int) INTVAL (op);
  2856. #endif
  2857.       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
  2858.     }
  2859.  
  2860.   else if (code == FLOAT && GET_CODE (op) == CONST_DOUBLE
  2861.        && GET_MODE (op) == VOIDmode)
  2862.     {
  2863.       REAL_VALUE_TYPE d;
  2864.  
  2865. #ifdef REAL_ARITHMETIC
  2866.       REAL_VALUE_FROM_INT (d, CONST_DOUBLE_LOW (op), CONST_DOUBLE_HIGH (op));
  2867. #else
  2868.       if (CONST_DOUBLE_HIGH (op) < 0)
  2869.     {
  2870.       d = (double) (~ CONST_DOUBLE_HIGH (op));
  2871.       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
  2872.         * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
  2873.       d += (double) (unsigned HOST_WIDE_INT) (~ CONST_DOUBLE_LOW (op));
  2874.       d = (- d - 1.0);
  2875.     }
  2876.       else
  2877.     {
  2878.       d = (double) CONST_DOUBLE_HIGH (op);
  2879.       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
  2880.         * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
  2881.       d += (double) (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (op);
  2882.     }
  2883. #endif  /* REAL_ARITHMETIC */
  2884.       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
  2885.     }
  2886.   else if (code == UNSIGNED_FLOAT && GET_CODE (op) == CONST_DOUBLE
  2887.        && GET_MODE (op) == VOIDmode)
  2888.     {
  2889.       REAL_VALUE_TYPE d;
  2890.  
  2891. #ifdef REAL_ARITHMETIC
  2892.       REAL_VALUE_FROM_UNSIGNED_INT (d, CONST_DOUBLE_LOW (op),
  2893.                     CONST_DOUBLE_HIGH (op));
  2894. #else
  2895.       d = (double) CONST_DOUBLE_HIGH (op);
  2896.       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
  2897.         * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
  2898.       d += (double) (unsigned HOST_WIDE_INT) CONST_DOUBLE_LOW (op);
  2899. #endif  /* REAL_ARITHMETIC */
  2900.       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
  2901.     }
  2902. #endif
  2903.  
  2904.   if (GET_CODE (op) == CONST_INT
  2905.       && width <= HOST_BITS_PER_WIDE_INT && width > 0)
  2906.     {
  2907.       register HOST_WIDE_INT arg0 = INTVAL (op);
  2908.       register HOST_WIDE_INT val;
  2909.  
  2910.       switch (code)
  2911.     {
  2912.     case NOT:
  2913.       val = ~ arg0;
  2914.       break;
  2915.  
  2916.     case NEG:
  2917.       val = - arg0;
  2918.       break;
  2919.  
  2920.     case ABS:
  2921.       val = (arg0 >= 0 ? arg0 : - arg0);
  2922.       break;
  2923.  
  2924.     case FFS:
  2925.       /* Don't use ffs here.  Instead, get low order bit and then its
  2926.          number.  If arg0 is zero, this will return 0, as desired.  */
  2927.       arg0 &= GET_MODE_MASK (mode);
  2928.       val = exact_log2 (arg0 & (- arg0)) + 1;
  2929.       break;
  2930.  
  2931.     case TRUNCATE:
  2932.       val = arg0;
  2933.       break;
  2934.  
  2935.     case ZERO_EXTEND:
  2936.       if (op_mode == VOIDmode)
  2937.         op_mode = mode;
  2938.       if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
  2939.         {
  2940.           /* If we were really extending the mode,
  2941.          we would have to distinguish between zero-extension
  2942.          and sign-extension.  */
  2943.           if (width != GET_MODE_BITSIZE (op_mode))
  2944.         abort ();
  2945.           val = arg0;
  2946.         }
  2947.       else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
  2948.         val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
  2949.       else
  2950.         return 0;
  2951.       break;
  2952.  
  2953.     case SIGN_EXTEND:
  2954.       if (op_mode == VOIDmode)
  2955.         op_mode = mode;
  2956.       if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
  2957.         {
  2958.           /* If we were really extending the mode,
  2959.          we would have to distinguish between zero-extension
  2960.          and sign-extension.  */
  2961.           if (width != GET_MODE_BITSIZE (op_mode))
  2962.         abort ();
  2963.           val = arg0;
  2964.         }
  2965.       else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
  2966.         {
  2967.           val
  2968.         = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
  2969.           if (val
  2970.           & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
  2971.         val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
  2972.         }
  2973.       else
  2974.         return 0;
  2975.       break;
  2976.  
  2977.     case SQRT:
  2978.       return 0;
  2979.  
  2980.     default:
  2981.       abort ();
  2982.     }
  2983.  
  2984.       /* Clear the bits that don't belong in our mode,
  2985.      unless they and our sign bit are all one.
  2986.      So we get either a reasonable negative value or a reasonable
  2987.      unsigned value for this mode.  */
  2988.       if (width < HOST_BITS_PER_WIDE_INT
  2989.       && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
  2990.           != ((HOST_WIDE_INT) (-1) << (width - 1))))
  2991.     val &= (1 << width) - 1;
  2992.  
  2993.       return GEN_INT (val);
  2994.     }
  2995.  
  2996.   /* We can do some operations on integer CONST_DOUBLEs.  Also allow
  2997.      for a DImode operation on a CONST_INT. */
  2998.   else if (GET_MODE (op) == VOIDmode
  2999.        && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
  3000.     {
  3001.       HOST_WIDE_INT l1, h1, lv, hv;
  3002.  
  3003.       if (GET_CODE (op) == CONST_DOUBLE)
  3004.     l1 = CONST_DOUBLE_LOW (op), h1 = CONST_DOUBLE_HIGH (op);
  3005.       else
  3006.     l1 = INTVAL (op), h1 = l1 < 0 ? -1 : 0;
  3007.  
  3008.       switch (code)
  3009.     {
  3010.     case NOT:
  3011.       lv = ~ l1;
  3012.       hv = ~ h1;
  3013.       break;
  3014.  
  3015.     case NEG:
  3016.       neg_double (l1, h1, &lv, &hv);
  3017.       break;
  3018.  
  3019.     case ABS:
  3020.       if (h1 < 0)
  3021.         neg_double (l1, h1, &lv, &hv);
  3022.       else
  3023.         lv = l1, hv = h1;
  3024.       break;
  3025.  
  3026.     case FFS:
  3027.       hv = 0;
  3028.       if (l1 == 0)
  3029.         lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
  3030.       else
  3031.         lv = exact_log2 (l1 & (-l1)) + 1;
  3032.       break;
  3033.  
  3034.     case TRUNCATE:
  3035.       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
  3036.         return GEN_INT (l1 & GET_MODE_MASK (mode));
  3037.       else
  3038.         return 0;
  3039.       break;
  3040.  
  3041.     case ZERO_EXTEND:
  3042.       if (op_mode == VOIDmode
  3043.           || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
  3044.         return 0;
  3045.  
  3046.       hv = 0;
  3047.       lv = l1 & GET_MODE_MASK (op_mode);
  3048.       break;
  3049.  
  3050.     case SIGN_EXTEND:
  3051.       if (op_mode == VOIDmode
  3052.           || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
  3053.         return 0;
  3054.       else
  3055.         {
  3056.           lv = l1 & GET_MODE_MASK (op_mode);
  3057.           if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
  3058.           && (lv & ((HOST_WIDE_INT) 1
  3059.                 << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
  3060.         lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
  3061.  
  3062.           hv = (lv < 0) ? ~ (HOST_WIDE_INT) 0 : 0;
  3063.         }
  3064.       break;
  3065.  
  3066.     case SQRT:
  3067.       return 0;
  3068.  
  3069.     default:
  3070.       return 0;
  3071.     }
  3072.  
  3073.       return immed_double_const (lv, hv, mode);
  3074.     }
  3075.  
  3076. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3077.   else if (GET_CODE (op) == CONST_DOUBLE
  3078.        && GET_MODE_CLASS (mode) == MODE_FLOAT)
  3079.     {
  3080.       REAL_VALUE_TYPE d;
  3081.       jmp_buf handler;
  3082.       rtx x;
  3083.  
  3084.       if (setjmp (handler))
  3085.     /* There used to be a warning here, but that is inadvisable.
  3086.        People may want to cause traps, and the natural way
  3087.        to do it should not get a warning.  */
  3088.     return 0;
  3089.  
  3090.       set_float_handler (handler);
  3091.  
  3092.       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
  3093.  
  3094.       switch (code)
  3095.     {
  3096.     case NEG:
  3097.       d = REAL_VALUE_NEGATE (d);
  3098.       break;
  3099.  
  3100.     case ABS:
  3101.       if (REAL_VALUE_NEGATIVE (d))
  3102.         d = REAL_VALUE_NEGATE (d);
  3103.       break;
  3104.  
  3105.     case FLOAT_TRUNCATE:
  3106.       d = (double) real_value_truncate (mode, d);
  3107.       break;
  3108.  
  3109.     case FLOAT_EXTEND:
  3110.       /* All this does is change the mode.  */
  3111.       break;
  3112.  
  3113.     case FIX:
  3114.       d = (double) REAL_VALUE_FIX_TRUNCATE (d);
  3115.       break;
  3116.  
  3117.     case UNSIGNED_FIX:
  3118.       d = (double) REAL_VALUE_UNSIGNED_FIX_TRUNCATE (d);
  3119.       break;
  3120.  
  3121.     case SQRT:
  3122.       return 0;
  3123.  
  3124.     default:
  3125.       abort ();
  3126.     }
  3127.  
  3128.       x = immed_real_const_1 (d, mode);
  3129.       set_float_handler (NULL_PTR);
  3130.       return x;
  3131.     }
  3132.   else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE_CLASS (mode) == MODE_INT
  3133.        && width <= HOST_BITS_PER_WIDE_INT && width > 0)
  3134.     {
  3135.       REAL_VALUE_TYPE d;
  3136.       jmp_buf handler;
  3137.       rtx x;
  3138.       HOST_WIDE_INT val;
  3139.  
  3140.       if (setjmp (handler))
  3141.     return 0;
  3142.  
  3143.       set_float_handler (handler);
  3144.  
  3145.       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
  3146.  
  3147.       switch (code)
  3148.     {
  3149.     case FIX:
  3150.       val = REAL_VALUE_FIX (d);
  3151.       break;
  3152.  
  3153.     case UNSIGNED_FIX:
  3154.       val = REAL_VALUE_UNSIGNED_FIX (d);
  3155.       break;
  3156.  
  3157.     default:
  3158.       abort ();
  3159.     }
  3160.  
  3161.       set_float_handler (NULL_PTR);
  3162.  
  3163.       /* Clear the bits that don't belong in our mode,
  3164.      unless they and our sign bit are all one.
  3165.      So we get either a reasonable negative value or a reasonable
  3166.      unsigned value for this mode.  */
  3167.       if (width < HOST_BITS_PER_WIDE_INT
  3168.       && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
  3169.           != ((HOST_WIDE_INT) (-1) << (width - 1))))
  3170.     val &= ((HOST_WIDE_INT) 1 << width) - 1;
  3171.  
  3172.       return GEN_INT (val);
  3173.     }
  3174. #endif
  3175.   /* This was formerly used only for non-IEEE float.
  3176.      eggert@twinsun.com says it is safe for IEEE also.  */
  3177.   else
  3178.     {
  3179.       /* There are some simplifications we can do even if the operands
  3180.      aren't constant.  */
  3181.       switch (code)
  3182.     {
  3183.     case NEG:
  3184.     case NOT:
  3185.       /* (not (not X)) == X, similarly for NEG.  */
  3186.       if (GET_CODE (op) == code)
  3187.         return XEXP (op, 0);
  3188.       break;
  3189.  
  3190.     case SIGN_EXTEND:
  3191.       /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
  3192.          becomes just the MINUS if its mode is MODE.  This allows
  3193.          folding switch statements on machines using casesi (such as
  3194.          the Vax).  */
  3195.       if (GET_CODE (op) == TRUNCATE
  3196.           && GET_MODE (XEXP (op, 0)) == mode
  3197.           && GET_CODE (XEXP (op, 0)) == MINUS
  3198.           && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
  3199.           && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
  3200.         return XEXP (op, 0);
  3201.       break;
  3202.     }
  3203.  
  3204.       return 0;
  3205.     }
  3206. }
  3207.  
  3208. /* Simplify a binary operation CODE with result mode MODE, operating on OP0
  3209.    and OP1.  Return 0 if no simplification is possible.
  3210.  
  3211.    Don't use this for relational operations such as EQ or LT.
  3212.    Use simplify_relational_operation instead.  */
  3213.  
  3214. rtx
  3215. simplify_binary_operation (code, mode, op0, op1)
  3216.      enum rtx_code code;
  3217.      enum machine_mode mode;
  3218.      rtx op0, op1;
  3219. {
  3220.   register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
  3221.   HOST_WIDE_INT val;
  3222.   int width = GET_MODE_BITSIZE (mode);
  3223.  
  3224.   /* Relational operations don't work here.  We must know the mode
  3225.      of the operands in order to do the comparison correctly.
  3226.      Assuming a full word can give incorrect results.
  3227.      Consider comparing 128 with -128 in QImode.  */
  3228.  
  3229.   if (GET_RTX_CLASS (code) == '<')
  3230.     abort ();
  3231.  
  3232. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3233.   if (GET_MODE_CLASS (mode) == MODE_FLOAT
  3234.       && GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
  3235.       && mode == GET_MODE (op0) && mode == GET_MODE (op1))
  3236.     {
  3237.       REAL_VALUE_TYPE f0, f1, value;
  3238.       jmp_buf handler;
  3239.  
  3240.       if (setjmp (handler))
  3241.     return 0;
  3242.  
  3243.       set_float_handler (handler);
  3244.  
  3245.       REAL_VALUE_FROM_CONST_DOUBLE (f0, op0);
  3246.       REAL_VALUE_FROM_CONST_DOUBLE (f1, op1);
  3247.       f0 = real_value_truncate (mode, f0);
  3248.       f1 = real_value_truncate (mode, f1);
  3249.  
  3250. #if 0/*def REAL_ARITHMETIC*/
  3251.       REAL_ARITHMETIC (value, code, f0, f1);
  3252. #else
  3253.       switch (code)
  3254.     {
  3255.     case PLUS:
  3256.       value = f0 + f1;
  3257.       break;
  3258.     case MINUS:
  3259.       value = f0 - f1;
  3260.       break;
  3261.     case MULT:
  3262.       value = f0 * f1;
  3263.       break;
  3264.     case DIV:
  3265. #ifndef REAL_INFINITY
  3266.       if (f1 == 0)
  3267.         return 0;
  3268. #endif
  3269.       value = f0 / f1;
  3270.       break;
  3271.     case SMIN:
  3272.       value = MIN (f0, f1);
  3273.       break;
  3274.     case SMAX:
  3275.       value = MAX (f0, f1);
  3276.       break;
  3277.     default:
  3278.       abort ();
  3279.     }
  3280. #endif
  3281.  
  3282.       set_float_handler (NULL_PTR);
  3283.       value = real_value_truncate (mode, value);
  3284.       return immed_real_const_1 (value, mode);
  3285.     }
  3286.  
  3287.   /* We can fold some multi-word operations.  */
  3288.   else if (GET_MODE_CLASS (mode) == MODE_INT
  3289.        && GET_CODE (op0) == CONST_DOUBLE
  3290.        && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
  3291.     {
  3292.       HOST_WIDE_INT l1, l2, h1, h2, lv, hv;
  3293.  
  3294.       l1 = CONST_DOUBLE_LOW (op0), h1 = CONST_DOUBLE_HIGH (op0);
  3295.  
  3296.       if (GET_CODE (op1) == CONST_DOUBLE)
  3297.     l2 = CONST_DOUBLE_LOW (op1), h2 = CONST_DOUBLE_HIGH (op1);
  3298.       else
  3299.     l2 = INTVAL (op1), h2 = l2 < 0 ? -1 : 0;
  3300.  
  3301.       switch (code)
  3302.     {
  3303.     case MINUS:
  3304.       /* A - B == A + (-B).  */
  3305.       neg_double (l2, h2, &lv, &hv);
  3306.       l2 = lv, h2 = hv;
  3307.  
  3308.       /* .. fall through ... */
  3309.  
  3310.     case PLUS:
  3311.       add_double (l1, h1, l2, h2, &lv, &hv);
  3312.       break;
  3313.  
  3314.     case MULT:
  3315.       mul_double (l1, h1, l2, h2, &lv, &hv);
  3316.       break;
  3317.  
  3318.     case DIV:  case MOD:   case UDIV:  case UMOD:
  3319.       /* We'd need to include tree.h to do this and it doesn't seem worth
  3320.          it.  */
  3321.       return 0;
  3322.  
  3323.     case AND:
  3324.       lv = l1 & l2, hv = h1 & h2;
  3325.       break;
  3326.  
  3327.     case IOR:
  3328.       lv = l1 | l2, hv = h1 | h2;
  3329.       break;
  3330.  
  3331.     case XOR:
  3332.       lv = l1 ^ l2, hv = h1 ^ h2;
  3333.       break;
  3334.  
  3335.     case SMIN:
  3336.       if (h1 < h2
  3337.           || (h1 == h2
  3338.           && ((unsigned HOST_WIDE_INT) l1
  3339.               < (unsigned HOST_WIDE_INT) l2)))
  3340.         lv = l1, hv = h1;
  3341.       else
  3342.         lv = l2, hv = h2;
  3343.       break;
  3344.  
  3345.     case SMAX:
  3346.       if (h1 > h2
  3347.           || (h1 == h2
  3348.           && ((unsigned HOST_WIDE_INT) l1
  3349.               > (unsigned HOST_WIDE_INT) l2)))
  3350.         lv = l1, hv = h1;
  3351.       else
  3352.         lv = l2, hv = h2;
  3353.       break;
  3354.  
  3355.     case UMIN:
  3356.       if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
  3357.           || (h1 == h2
  3358.           && ((unsigned HOST_WIDE_INT) l1
  3359.               < (unsigned HOST_WIDE_INT) l2)))
  3360.         lv = l1, hv = h1;
  3361.       else
  3362.         lv = l2, hv = h2;
  3363.       break;
  3364.  
  3365.     case UMAX:
  3366.       if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
  3367.           || (h1 == h2
  3368.           && ((unsigned HOST_WIDE_INT) l1
  3369.               > (unsigned HOST_WIDE_INT) l2)))
  3370.         lv = l1, hv = h1;
  3371.       else
  3372.         lv = l2, hv = h2;
  3373.       break;
  3374.  
  3375.     case LSHIFTRT:   case ASHIFTRT:
  3376.     case ASHIFT:     case LSHIFT:
  3377.     case ROTATE:     case ROTATERT:
  3378. #ifdef SHIFT_COUNT_TRUNCATED
  3379.       l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
  3380. #endif
  3381.  
  3382.       if (h2 != 0 || l2 < 0 || l2 >= GET_MODE_BITSIZE (mode))
  3383.         return 0;
  3384.  
  3385.       if (code == LSHIFTRT || code == ASHIFTRT)
  3386.         rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
  3387.                code == ASHIFTRT);
  3388.       else if (code == ASHIFT || code == LSHIFT)
  3389.         lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
  3390.                code == ASHIFT);
  3391.       else if (code == ROTATE)
  3392.         lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
  3393.       else /* code == ROTATERT */
  3394.         rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
  3395.       break;
  3396.  
  3397.     default:
  3398.       return 0;
  3399.     }
  3400.  
  3401.       return immed_double_const (lv, hv, mode);
  3402.     }
  3403. #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  3404.  
  3405.   if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
  3406.       || width > HOST_BITS_PER_WIDE_INT || width == 0)
  3407.     {
  3408.       /* Even if we can't compute a constant result,
  3409.      there are some cases worth simplifying.  */
  3410.  
  3411.       switch (code)
  3412.     {
  3413.     case PLUS:
  3414.       /* In IEEE floating point, x+0 is not the same as x.  Similarly
  3415.          for the other optimizations below.  */
  3416.       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
  3417.           && GET_MODE_CLASS (mode) != MODE_INT)
  3418.         break;
  3419.  
  3420.       if (op1 == CONST0_RTX (mode))
  3421.         return op0;
  3422.  
  3423.       /* Strip off any surrounding CONSTs.  They don't matter in any of 
  3424.          the cases below.  */
  3425.       if (GET_CODE (op0) == CONST)
  3426.         op0 = XEXP (op0, 0);
  3427.       if (GET_CODE (op1) == CONST)
  3428.         op1 = XEXP (op1, 0);
  3429.  
  3430.       /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
  3431.       if (GET_CODE (op0) == NEG)
  3432.         {
  3433.           rtx tem = simplify_binary_operation (MINUS, mode,
  3434.                            op1, XEXP (op0, 0));
  3435.           return tem ? tem : gen_rtx (MINUS, mode, op1, XEXP (op0, 0));
  3436.         }
  3437.       else if (GET_CODE (op1) == NEG)
  3438.         {
  3439.           rtx tem = simplify_binary_operation (MINUS, mode,
  3440.                            op0, XEXP (op1, 0));
  3441.           return tem ? tem : gen_rtx (MINUS, mode, op0, XEXP (op1, 0));
  3442.         }
  3443.  
  3444.       /* Don't use the associative law for floating point.
  3445.          The inaccuracy makes it nonassociative,
  3446.          and subtle programs can break if operations are associated.  */
  3447.       if (GET_MODE_CLASS (mode) != MODE_INT)
  3448.         break;
  3449.  
  3450.       /* (a - b) + b -> a, similarly a + (b - a) -> a */
  3451.       if (GET_CODE (op0) == MINUS
  3452.           && rtx_equal_p (XEXP (op0, 1), op1) && ! side_effects_p (op1))
  3453.         return XEXP (op0, 0);
  3454.  
  3455.       if (GET_CODE (op1) == MINUS
  3456.           && rtx_equal_p (XEXP (op1, 1), op0) && ! side_effects_p (op0))
  3457.         return XEXP (op1, 0);
  3458.  
  3459.       /* (c1 - a) + c2 becomes (c1 + c2) - a.  */
  3460.       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == MINUS
  3461.           && GET_CODE (XEXP (op0, 0)) == CONST_INT)
  3462.         {
  3463.           rtx tem = simplify_binary_operation (PLUS, mode, op1,
  3464.                            XEXP (op0, 0));
  3465.  
  3466.           return tem ? gen_rtx (MINUS, mode, tem, XEXP (op0, 1)) : 0;
  3467.         }
  3468.  
  3469.       /* Handle both-operands-constant cases.  */
  3470.       if (CONSTANT_P (op0) && CONSTANT_P (op1)
  3471.           && GET_CODE (op0) != CONST_DOUBLE
  3472.           && GET_CODE (op1) != CONST_DOUBLE
  3473.           && GET_MODE_CLASS (mode) == MODE_INT)
  3474.         {
  3475.           if (GET_CODE (op1) == CONST_INT)
  3476.         return plus_constant (op0, INTVAL (op1));
  3477.           else if (GET_CODE (op0) == CONST_INT)
  3478.         return plus_constant (op1, INTVAL (op0));
  3479.           else
  3480.         break;
  3481. #if 0 /* No good, because this can produce the sum of two relocatable
  3482.      symbols, in an assembler instruction.  Most UNIX assemblers can't
  3483.      handle that.  */
  3484.           else
  3485.         return gen_rtx (CONST, mode,
  3486.                 gen_rtx (PLUS, mode,
  3487.                      GET_CODE (op0) == CONST
  3488.                      ? XEXP (op0, 0) : op0,
  3489.                      GET_CODE (op1) == CONST
  3490.                      ? XEXP (op1, 0) : op1));
  3491. #endif
  3492.         }
  3493.       else if (GET_CODE (op1) == CONST_INT
  3494.            && GET_CODE (op0) == PLUS
  3495.            && (CONSTANT_P (XEXP (op0, 0))
  3496.                || CONSTANT_P (XEXP (op0, 1))))
  3497.         /* constant + (variable + constant)
  3498.            can result if an index register is made constant.
  3499.            We simplify this by adding the constants.
  3500.            If we did not, it would become an invalid address.  */
  3501.         return plus_constant (op0, INTVAL (op1));
  3502.       break;
  3503.  
  3504.     case COMPARE:
  3505. #ifdef HAVE_cc0
  3506.       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
  3507.          using cc0, in which case we want to leave it as a COMPARE
  3508.          so we can distinguish it from a register-register-copy.
  3509.  
  3510.          In IEEE floating point, x-0 is not the same as x.  */
  3511.  
  3512.       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3513.            || GET_MODE_CLASS (mode) == MODE_INT)
  3514.           && op1 == CONST0_RTX (mode))
  3515.         return op0;
  3516. #else
  3517.       /* Do nothing here.  */
  3518. #endif
  3519.       break;
  3520.           
  3521.     case MINUS:
  3522.       /* None of these optimizations can be done for IEEE
  3523.          floating point.  */
  3524.       if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
  3525.           && GET_MODE_CLASS (mode) != MODE_INT)
  3526.         break;
  3527.  
  3528.       /* We can't assume x-x is 0 even with non-IEEE floating point.  */
  3529.       if (rtx_equal_p (op0, op1)
  3530.           && ! side_effects_p (op0)
  3531.           && GET_MODE_CLASS (mode) != MODE_FLOAT)
  3532.         return const0_rtx;
  3533.  
  3534.       /* Change subtraction from zero into negation.  */
  3535.       if (op0 == CONST0_RTX (mode))
  3536.         return gen_rtx (NEG, mode, op1);
  3537.  
  3538.       /* Subtracting 0 has no effect.  */
  3539.       if (op1 == CONST0_RTX (mode))
  3540.         return op0;
  3541.  
  3542.       /* Strip off any surrounding CONSTs.  They don't matter in any of 
  3543.          the cases below.  */
  3544.       if (GET_CODE (op0) == CONST)
  3545.         op0 = XEXP (op0, 0);
  3546.       if (GET_CODE (op1) == CONST)
  3547.         op1 = XEXP (op1, 0);
  3548.  
  3549.       /* (a - (-b)) -> (a + b).  */
  3550.       if (GET_CODE (op1) == NEG)
  3551.         {
  3552.           rtx tem = simplify_binary_operation (PLUS, mode,
  3553.                            op0, XEXP (op1, 0));
  3554.           return tem ? tem : gen_rtx (PLUS, mode, op0, XEXP (op1, 0));
  3555.         }
  3556.  
  3557.       /* Don't use the associative law for floating point.
  3558.          The inaccuracy makes it nonassociative,
  3559.          and subtle programs can break if operations are associated.  */
  3560.       if (GET_MODE_CLASS (mode) != MODE_INT)
  3561.         break;
  3562.  
  3563.       /* (a + b) - a -> b, and (b - (a + b))  -> -a  */
  3564.       if (GET_CODE (op0) == PLUS
  3565.           && rtx_equal_p (XEXP (op0, 0), op1)
  3566.           && ! side_effects_p (op1))
  3567.         return XEXP (op0, 1);
  3568.       else if (GET_CODE (op0) == PLUS
  3569.            && rtx_equal_p (XEXP (op0, 1), op1)
  3570.            && ! side_effects_p (op1))
  3571.         return XEXP (op0, 0);
  3572.  
  3573.       if (GET_CODE (op1) == PLUS
  3574.           && rtx_equal_p (XEXP (op1, 0), op0)
  3575.           && ! side_effects_p (op0))
  3576.         {
  3577.           rtx tem = simplify_unary_operation (NEG, mode, XEXP (op1, 1),
  3578.                           mode);
  3579.  
  3580.           return tem ? tem : gen_rtx (NEG, mode, XEXP (op1, 1));
  3581.         }
  3582.       else if (GET_CODE (op1) == PLUS
  3583.            && rtx_equal_p (XEXP (op1, 1), op0)
  3584.            && ! side_effects_p (op0))
  3585.         {
  3586.           rtx tem = simplify_unary_operation (NEG, mode, XEXP (op1, 0),
  3587.                           mode);
  3588.  
  3589.           return tem ? tem : gen_rtx (NEG, mode, XEXP (op1, 0));
  3590.         }
  3591.  
  3592.       /* a - (a - b) -> b */
  3593.       if (GET_CODE (op1) == MINUS && rtx_equal_p (op0, XEXP (op1, 0))
  3594.           && ! side_effects_p (op0))
  3595.         return XEXP (op1, 1);
  3596.  
  3597.       /* (a +/- b) - (a +/- c) can be simplified.  Do variants of
  3598.          this involving commutativity.  The most common case is
  3599.          (a + C1) - (a + C2), but it's not hard to do all the cases.  */
  3600.       if ((GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS)
  3601.           && (GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS))
  3602.         {
  3603.           rtx lhs0 = XEXP (op0, 0), lhs1 = XEXP (op0, 1);
  3604.           rtx rhs0 = XEXP (op1, 0), rhs1 = XEXP (op1, 1);
  3605.           int lhs_neg = GET_CODE (op0) == MINUS;
  3606.           int rhs_neg = GET_CODE (op1) == MINUS;
  3607.           rtx lhs = 0, rhs = 0;
  3608.  
  3609.           /* Set LHS and RHS to the two different terms.  */
  3610.           if (rtx_equal_p (lhs0, rhs0) && ! side_effects_p (lhs0))
  3611.         lhs = lhs1, rhs = rhs1;
  3612.           else if (! rhs_neg && rtx_equal_p (lhs0, rhs1)
  3613.                && ! side_effects_p (lhs0))
  3614.         lhs = lhs1, rhs = rhs0;
  3615.           else if (! lhs_neg && rtx_equal_p (lhs1, rhs0)
  3616.                && ! side_effects_p (lhs1))
  3617.         lhs = lhs0, rhs = rhs1;
  3618.           else if (! lhs_neg && ! rhs_neg && rtx_equal_p (lhs1, rhs1)
  3619.                && ! side_effects_p (lhs1))
  3620.         lhs = lhs0, rhs = rhs0;
  3621.  
  3622.           /* The RHS is the operand of a MINUS, so its negation
  3623.          status should be complemented.  */
  3624.           rhs_neg = ! rhs_neg;
  3625.  
  3626.           /* If we found two values equal, form the sum or difference
  3627.          of the remaining two terms.   */
  3628.           if (lhs)
  3629.         {
  3630.           rtx tem = simplify_binary_operation (lhs_neg == rhs_neg
  3631.                                ? PLUS : MINUS,
  3632.                                mode,
  3633.                                lhs_neg ? rhs : lhs,
  3634.                                lhs_neg ? lhs : rhs);
  3635.           if (tem == 0)
  3636.             tem = gen_rtx (lhs_neg == rhs_neg
  3637.                    ? PLUS : MINUS,
  3638.                    mode, lhs_neg ? rhs : lhs,
  3639.                    lhs_neg ? lhs : rhs);
  3640.  
  3641.           /* If both sides negated, negate result.  */
  3642.           if (lhs_neg && rhs_neg)
  3643.             {
  3644.               rtx tem1
  3645.             = simplify_unary_operation (NEG, mode, tem, mode);
  3646.               if (tem1 == 0)
  3647.             tem1 = gen_rtx (NEG, mode, tem);
  3648.               tem = tem1;
  3649.             }
  3650.  
  3651.           return tem;
  3652.         }
  3653.  
  3654.           return 0;
  3655.         }
  3656.  
  3657.       /* c1 - (a + c2) becomes (c1 - c2) - a.  */
  3658.       if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == PLUS
  3659.           && GET_CODE (XEXP (op1, 1)) == CONST_INT)
  3660.         {
  3661.           rtx tem = simplify_binary_operation (MINUS, mode, op0,
  3662.                            XEXP (op1, 1));
  3663.  
  3664.           return tem ? gen_rtx (MINUS, mode, tem, XEXP (op1, 0)) : 0;
  3665.         }
  3666.  
  3667.       /* c1 - (c2 - a) becomes (c1 - c2) + a.  */
  3668.       if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == MINUS
  3669.           && GET_CODE (XEXP (op1, 0)) == CONST_INT)
  3670.         {
  3671.           rtx tem = simplify_binary_operation (MINUS, mode, op0,
  3672.                            XEXP (op1, 0));
  3673.  
  3674.           return (tem && GET_CODE (tem) == CONST_INT
  3675.               ? plus_constant (XEXP (op1, 1), INTVAL (tem))
  3676.               : 0);
  3677.         }
  3678.  
  3679.       /* Don't let a relocatable value get a negative coeff.  */
  3680.       if (GET_CODE (op1) == CONST_INT)
  3681.         return plus_constant (op0, - INTVAL (op1));
  3682.       break;
  3683.  
  3684.     case MULT:
  3685.       if (op1 == constm1_rtx)
  3686.         {
  3687.           rtx tem = simplify_unary_operation (NEG, mode, op0, mode);
  3688.  
  3689.           return tem ? tem : gen_rtx (NEG, mode, op0);
  3690.         }
  3691.  
  3692.       /* In IEEE floating point, x*0 is not always 0.  */
  3693.       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3694.            || GET_MODE_CLASS (mode) == MODE_INT)
  3695.           && op1 == CONST0_RTX (mode)
  3696.           && ! side_effects_p (op0))
  3697.         return op1;
  3698.  
  3699.       /* In IEEE floating point, x*1 is not equivalent to x for nans.
  3700.          However, ANSI says we can drop signals,
  3701.          so we can do this anyway.  */
  3702.       if (op1 == CONST1_RTX (mode))
  3703.         return op0;
  3704.  
  3705.       /* Convert multiply by constant power of two into shift.  */
  3706.       if (GET_CODE (op1) == CONST_INT
  3707.           && (val = exact_log2 (INTVAL (op1))) >= 0)
  3708.         return gen_rtx (ASHIFT, mode, op0, GEN_INT (val));
  3709.  
  3710.       if (GET_CODE (op1) == CONST_DOUBLE
  3711.           && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT)
  3712.         {
  3713.           REAL_VALUE_TYPE d;
  3714.           REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
  3715.  
  3716.           /* x*2 is x+x and x*(-1) is -x */
  3717.           if (REAL_VALUES_EQUAL (d, dconst2)
  3718.           && GET_MODE (op0) == mode)
  3719.         return gen_rtx (PLUS, mode, op0, copy_rtx (op0));
  3720.  
  3721.           else if (REAL_VALUES_EQUAL (d, dconstm1)
  3722.                && GET_MODE (op0) == mode)
  3723.         return gen_rtx (NEG, mode, op0);
  3724.         }
  3725.       break;
  3726.  
  3727.     case IOR:
  3728.       if (op1 == const0_rtx)
  3729.         return op0;
  3730.       if (GET_CODE (op1) == CONST_INT
  3731.           && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
  3732.         return op1;
  3733.       if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
  3734.         return op0;
  3735.       /* A | (~A) -> -1 */
  3736.       if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
  3737.            || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
  3738.           && ! side_effects_p (op0))
  3739.         return constm1_rtx;
  3740.       break;
  3741.  
  3742.     case XOR:
  3743.       if (op1 == const0_rtx)
  3744.         return op0;
  3745.       if (GET_CODE (op1) == CONST_INT
  3746.           && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
  3747.         return gen_rtx (NOT, mode, op0);
  3748.       if (op0 == op1 && ! side_effects_p (op0))
  3749.         return const0_rtx;
  3750.       break;
  3751.  
  3752.     case AND:
  3753.       if (op1 == const0_rtx && ! side_effects_p (op0))
  3754.         return const0_rtx;
  3755.       if (GET_CODE (op1) == CONST_INT
  3756.           && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
  3757.         return op0;
  3758.       if (op0 == op1 && ! side_effects_p (op0))
  3759.         return op0;
  3760.       /* A & (~A) -> 0 */
  3761.       if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
  3762.            || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
  3763.           && ! side_effects_p (op0))
  3764.         return const0_rtx;
  3765.       break;
  3766.  
  3767.     case UDIV:
  3768.       /* Convert divide by power of two into shift (divide by 1 handled
  3769.          below).  */
  3770.       if (GET_CODE (op1) == CONST_INT
  3771.           && (arg1 = exact_log2 (INTVAL (op1))) > 0)
  3772.         return gen_rtx (LSHIFTRT, mode, op0, GEN_INT (arg1));
  3773.  
  3774.       /* ... fall through ... */
  3775.  
  3776.     case DIV:
  3777.       if (op1 == CONST1_RTX (mode))
  3778.         return op0;
  3779.  
  3780.       /* In IEEE floating point, 0/x is not always 0.  */
  3781.       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3782.            || GET_MODE_CLASS (mode) == MODE_INT)
  3783.           && op0 == CONST0_RTX (mode)
  3784.           && ! side_effects_p (op1))
  3785.         return op0;
  3786.  
  3787. #if 0 /* Turned off till an expert says this is a safe thing to do.  */
  3788. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3789.       /* Change division by a constant into multiplication.  */
  3790.       else if (GET_CODE (op1) == CONST_DOUBLE
  3791.            && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
  3792.            && op1 != CONST0_RTX (mode))
  3793.         {
  3794.           REAL_VALUE_TYPE d;
  3795.           REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
  3796.           if (REAL_VALUES_EQUAL (d, dconst0))
  3797.         abort();
  3798. #if defined (REAL_ARITHMETIC)
  3799.           REAL_ARITHMETIC (d, RDIV_EXPR, dconst1, d);
  3800.           return gen_rtx (MULT, mode, op0, 
  3801.                   CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
  3802. #else
  3803.           return gen_rtx (MULT, mode, op0, 
  3804.                   CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
  3805.         }
  3806. #endif
  3807. #endif
  3808. #endif
  3809.       break;
  3810.  
  3811.     case UMOD:
  3812.       /* Handle modulus by power of two (mod with 1 handled below).  */
  3813.       if (GET_CODE (op1) == CONST_INT
  3814.           && exact_log2 (INTVAL (op1)) > 0)
  3815.         return gen_rtx (AND, mode, op0, GEN_INT (INTVAL (op1) - 1));
  3816.  
  3817.       /* ... fall through ... */
  3818.  
  3819.     case MOD:
  3820.       if ((op0 == const0_rtx || op1 == const1_rtx)
  3821.           && ! side_effects_p (op0) && ! side_effects_p (op1))
  3822.         return const0_rtx;
  3823.       break;
  3824.  
  3825.     case ROTATERT:
  3826.     case ROTATE:
  3827.       /* Rotating ~0 always results in ~0.  */
  3828.       if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
  3829.           && INTVAL (op0) == GET_MODE_MASK (mode)
  3830.           && ! side_effects_p (op1))
  3831.         return op0;
  3832.  
  3833.       /* ... fall through ... */
  3834.  
  3835.     case LSHIFT:
  3836.     case ASHIFT:
  3837.     case ASHIFTRT:
  3838.     case LSHIFTRT:
  3839.       if (op1 == const0_rtx)
  3840.         return op0;
  3841.       if (op0 == const0_rtx && ! side_effects_p (op1))
  3842.         return op0;
  3843.       break;
  3844.  
  3845.     case SMIN:
  3846.       if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT 
  3847.           && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1)
  3848.           && ! side_effects_p (op0))
  3849.         return op1;
  3850.       else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
  3851.         return op0;
  3852.       break;
  3853.        
  3854.     case SMAX:
  3855.       if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
  3856.           && INTVAL (op1) == GET_MODE_MASK (mode) >> 1
  3857.           && ! side_effects_p (op0))
  3858.         return op1;
  3859.       else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
  3860.         return op0;
  3861.       break;
  3862.  
  3863.     case UMIN:
  3864.       if (op1 == const0_rtx && ! side_effects_p (op0))
  3865.         return op1;
  3866.       else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
  3867.         return op0;
  3868.       break;
  3869.         
  3870.     case UMAX:
  3871.       if (op1 == constm1_rtx && ! side_effects_p (op0))
  3872.         return op1;
  3873.       else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
  3874.         return op0;
  3875.       break;
  3876.  
  3877.     default:
  3878.       abort ();
  3879.     }
  3880.       
  3881.       return 0;
  3882.     }
  3883.  
  3884.   /* Get the integer argument values in two forms:
  3885.      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
  3886.  
  3887.   arg0 = INTVAL (op0);
  3888.   arg1 = INTVAL (op1);
  3889.  
  3890.   if (width < HOST_BITS_PER_WIDE_INT)
  3891.     {
  3892.       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
  3893.       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
  3894.  
  3895.       arg0s = arg0;
  3896.       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
  3897.     arg0s |= ((HOST_WIDE_INT) (-1) << width);
  3898.  
  3899.       arg1s = arg1;
  3900.       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
  3901.     arg1s |= ((HOST_WIDE_INT) (-1) << width);
  3902.     }
  3903.   else
  3904.     {
  3905.       arg0s = arg0;
  3906.       arg1s = arg1;
  3907.     }
  3908.  
  3909.   /* Compute the value of the arithmetic.  */
  3910.  
  3911.   switch (code)
  3912.     {
  3913.     case PLUS:
  3914.       val = arg0s + arg1s;
  3915.       break;
  3916.  
  3917.     case MINUS:
  3918.       val = arg0s - arg1s;
  3919.       break;
  3920.  
  3921.     case MULT:
  3922.       val = arg0s * arg1s;
  3923.       break;
  3924.  
  3925.     case DIV:
  3926.       if (arg1s == 0)
  3927.     return 0;
  3928.       val = arg0s / arg1s;
  3929.       break;
  3930.  
  3931.     case MOD:
  3932.       if (arg1s == 0)
  3933.     return 0;
  3934.       val = arg0s % arg1s;
  3935.       break;
  3936.  
  3937.     case UDIV:
  3938.       if (arg1 == 0)
  3939.     return 0;
  3940.       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
  3941.       break;
  3942.  
  3943.     case UMOD:
  3944.       if (arg1 == 0)
  3945.     return 0;
  3946.       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
  3947.       break;
  3948.  
  3949.     case AND:
  3950.       val = arg0 & arg1;
  3951.       break;
  3952.  
  3953.     case IOR:
  3954.       val = arg0 | arg1;
  3955.       break;
  3956.  
  3957.     case XOR:
  3958.       val = arg0 ^ arg1;
  3959.       break;
  3960.  
  3961.     case LSHIFTRT:
  3962.       /* If shift count is undefined, don't fold it; let the machine do
  3963.      what it wants.  But truncate it if the machine will do that.  */
  3964.       if (arg1 < 0)
  3965.     return 0;
  3966.  
  3967. #ifdef SHIFT_COUNT_TRUNCATED
  3968.       arg1 &= (BITS_PER_WORD - 1);
  3969. #endif
  3970.  
  3971.       if (arg1 >= width)
  3972.     return 0;
  3973.  
  3974.       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
  3975.       break;
  3976.  
  3977.     case ASHIFT:
  3978.     case LSHIFT:
  3979.       if (arg1 < 0)
  3980.     return 0;
  3981.  
  3982. #ifdef SHIFT_COUNT_TRUNCATED
  3983.       arg1 &= (BITS_PER_WORD - 1);
  3984. #endif
  3985.  
  3986.       if (arg1 >= width)
  3987.     return 0;
  3988.  
  3989.       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
  3990.       break;
  3991.  
  3992.     case ASHIFTRT:
  3993.       if (arg1 < 0)
  3994.     return 0;
  3995.  
  3996. #ifdef SHIFT_COUNT_TRUNCATED
  3997.       arg1 &= (BITS_PER_WORD - 1);
  3998. #endif
  3999.  
  4000.       if (arg1 >= width)
  4001.     return 0;
  4002.  
  4003.       val = arg0s >> arg1;
  4004.  
  4005.       /* Bootstrap compiler may not have sign extended the right shift.
  4006.      Manually extend the sign to insure bootstrap cc matches gcc.  */
  4007.       if (arg0s < 0 && arg1 > 0)
  4008.     val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
  4009.  
  4010.       break;
  4011.  
  4012.     case ROTATERT:
  4013.       if (arg1 < 0)
  4014.     return 0;
  4015.  
  4016.       arg1 %= width;
  4017.       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
  4018.          | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
  4019.       break;
  4020.  
  4021.     case ROTATE:
  4022.       if (arg1 < 0)
  4023.     return 0;
  4024.  
  4025.       arg1 %= width;
  4026.       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
  4027.          | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
  4028.       break;
  4029.  
  4030.     case COMPARE:
  4031.       /* Do nothing here.  */
  4032.       return 0;
  4033.  
  4034.     case SMIN:
  4035.       val = arg0s <= arg1s ? arg0s : arg1s;
  4036.       break;
  4037.  
  4038.     case UMIN:
  4039.       val = ((unsigned HOST_WIDE_INT) arg0
  4040.          <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
  4041.       break;
  4042.  
  4043.     case SMAX:
  4044.       val = arg0s > arg1s ? arg0s : arg1s;
  4045.       break;
  4046.  
  4047.     case UMAX:
  4048.       val = ((unsigned HOST_WIDE_INT) arg0
  4049.          > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
  4050.       break;
  4051.  
  4052.     default:
  4053.       abort ();
  4054.     }
  4055.  
  4056.   /* Clear the bits that don't belong in our mode, unless they and our sign
  4057.      bit are all one.  So we get either a reasonable negative value or a
  4058.      reasonable unsigned value for this mode.  */
  4059.   if (width < HOST_BITS_PER_WIDE_INT
  4060.       && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
  4061.       != ((HOST_WIDE_INT) (-1) << (width - 1))))
  4062.     val &= ((HOST_WIDE_INT) 1 << width) - 1;
  4063.  
  4064.   return GEN_INT (val);
  4065. }
  4066.  
  4067. /* Like simplify_binary_operation except used for relational operators.
  4068.    MODE is the mode of the operands, not that of the result.  */
  4069.  
  4070. rtx
  4071. simplify_relational_operation (code, mode, op0, op1)
  4072.      enum rtx_code code;
  4073.      enum machine_mode mode;
  4074.      rtx op0, op1;
  4075. {
  4076.   register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
  4077.   HOST_WIDE_INT val;
  4078.   int width = GET_MODE_BITSIZE (mode);
  4079.  
  4080.   /* If op0 is a compare, extract the comparison arguments from it.  */
  4081.   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
  4082.     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
  4083.  
  4084.   if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
  4085.       || width > HOST_BITS_PER_WIDE_INT || width == 0)
  4086.     {
  4087.       /* Even if we can't compute a constant result,
  4088.      there are some cases worth simplifying.  */
  4089.  
  4090.       /* For non-IEEE floating-point, if the two operands are equal, we know
  4091.      the result.  */
  4092.       if (rtx_equal_p (op0, op1)
  4093.       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  4094.           || GET_MODE_CLASS (GET_MODE (op0)) != MODE_FLOAT))
  4095.     return (code == EQ || code == GE || code == LE || code == LEU
  4096.         || code == GEU) ? const_true_rtx : const0_rtx;
  4097.       else if (GET_CODE (op0) == CONST_DOUBLE
  4098.            && GET_CODE (op1) == CONST_DOUBLE
  4099.            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
  4100.     {
  4101.       REAL_VALUE_TYPE d0, d1;
  4102.       jmp_buf handler;
  4103.       int op0lt, op1lt, equal;
  4104.  
  4105.       if (setjmp (handler))
  4106.         return 0;
  4107.  
  4108.       set_float_handler (handler);
  4109.       REAL_VALUE_FROM_CONST_DOUBLE (d0, op0);
  4110.       REAL_VALUE_FROM_CONST_DOUBLE (d1, op1);
  4111.       equal = REAL_VALUES_EQUAL (d0, d1);
  4112.       op0lt = REAL_VALUES_LESS (d0, d1);
  4113.       op1lt = REAL_VALUES_LESS (d1, d0);
  4114.       set_float_handler (NULL_PTR);
  4115.  
  4116.       switch (code)
  4117.         {
  4118.         case EQ:
  4119.           return equal ? const_true_rtx : const0_rtx;
  4120.         case NE:
  4121.           return !equal ? const_true_rtx : const0_rtx;
  4122.         case LE:
  4123.           return equal || op0lt ? const_true_rtx : const0_rtx;
  4124.         case LT:
  4125.           return op0lt ? const_true_rtx : const0_rtx;
  4126.         case GE:
  4127.           return equal || op1lt ? const_true_rtx : const0_rtx;
  4128.         case GT:
  4129.           return op1lt ? const_true_rtx : const0_rtx;
  4130.         }
  4131.     }
  4132.       
  4133.       switch (code)
  4134.     {
  4135.     case EQ:
  4136.       {
  4137. #if 0
  4138.         /* We can't make this assumption due to #pragma weak */
  4139.         if (CONSTANT_P (op0) && op1 == const0_rtx)
  4140.           return const0_rtx;
  4141. #endif
  4142.         if (NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx
  4143.         /* On some machines, the ap reg can be 0 sometimes.  */
  4144.         && op0 != arg_pointer_rtx)
  4145.           return const0_rtx;
  4146.         break;
  4147.       }
  4148.  
  4149.     case NE:
  4150. #if 0
  4151.       /* We can't make this assumption due to #pragma weak */
  4152.       if (CONSTANT_P (op0) && op1 == const0_rtx)
  4153.         return const_true_rtx;
  4154. #endif
  4155.       if (NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx
  4156.           /* On some machines, the ap reg can be 0 sometimes.  */
  4157.           && op0 != arg_pointer_rtx)
  4158.         return const_true_rtx;
  4159.       break;
  4160.  
  4161.     case GEU:
  4162.       /* Unsigned values are never negative, but we must be sure we are
  4163.          actually comparing a value, not a CC operand.  */
  4164.       if (op1 == const0_rtx
  4165.           && GET_MODE_CLASS (mode) == MODE_INT)
  4166.         return const_true_rtx;
  4167.       break;
  4168.  
  4169.     case LTU:
  4170.       if (op1 == const0_rtx
  4171.           && GET_MODE_CLASS (mode) == MODE_INT)
  4172.         return const0_rtx;
  4173.       break;
  4174.  
  4175.     case LEU:
  4176.       /* Unsigned values are never greater than the largest
  4177.          unsigned value.  */
  4178.       if (GET_CODE (op1) == CONST_INT
  4179.           && INTVAL (op1) == GET_MODE_MASK (mode)
  4180.           && GET_MODE_CLASS (mode) == MODE_INT)
  4181.         return const_true_rtx;
  4182.       break;
  4183.  
  4184.     case GTU:
  4185.       if (GET_CODE (op1) == CONST_INT
  4186.           && INTVAL (op1) == GET_MODE_MASK (mode)
  4187.           && GET_MODE_CLASS (mode) == MODE_INT)
  4188.         return const0_rtx;
  4189.       break;
  4190.     }
  4191.  
  4192.       return 0;
  4193.     }
  4194.  
  4195.   /* Get the integer argument values in two forms:
  4196.      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
  4197.  
  4198.   arg0 = INTVAL (op0);
  4199.   arg1 = INTVAL (op1);
  4200.  
  4201.   if (width < HOST_BITS_PER_WIDE_INT)
  4202.     {
  4203.       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
  4204.       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
  4205.  
  4206.       arg0s = arg0;
  4207.       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
  4208.     arg0s |= ((HOST_WIDE_INT) (-1) << width);
  4209.  
  4210.       arg1s = arg1;
  4211.       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
  4212.     arg1s |= ((HOST_WIDE_INT) (-1) << width);
  4213.     }
  4214.   else
  4215.     {
  4216.       arg0s = arg0;
  4217.       arg1s = arg1;
  4218.     }
  4219.  
  4220.   /* Compute the value of the arithmetic.  */
  4221.  
  4222.   switch (code)
  4223.     {
  4224.     case NE:
  4225.       val = arg0 != arg1 ? STORE_FLAG_VALUE : 0;
  4226.       break;
  4227.  
  4228.     case EQ:
  4229.       val = arg0 == arg1 ? STORE_FLAG_VALUE : 0;
  4230.       break;
  4231.  
  4232.     case LE:
  4233.       val = arg0s <= arg1s ? STORE_FLAG_VALUE : 0;
  4234.       break;
  4235.  
  4236.     case LT:
  4237.       val = arg0s < arg1s ? STORE_FLAG_VALUE : 0;
  4238.       break;
  4239.  
  4240.     case GE:
  4241.       val = arg0s >= arg1s ? STORE_FLAG_VALUE : 0;
  4242.       break;
  4243.  
  4244.     case GT:
  4245.       val = arg0s > arg1s ? STORE_FLAG_VALUE : 0;
  4246.       break;
  4247.  
  4248.     case LEU:
  4249.       val = (((unsigned HOST_WIDE_INT) arg0)
  4250.          <= ((unsigned HOST_WIDE_INT) arg1) ? STORE_FLAG_VALUE : 0);
  4251.       break;
  4252.  
  4253.     case LTU:
  4254.       val = (((unsigned HOST_WIDE_INT) arg0)
  4255.          < ((unsigned HOST_WIDE_INT) arg1) ? STORE_FLAG_VALUE : 0);
  4256.       break;
  4257.  
  4258.     case GEU:
  4259.       val = (((unsigned HOST_WIDE_INT) arg0)
  4260.          >= ((unsigned HOST_WIDE_INT) arg1) ? STORE_FLAG_VALUE : 0);
  4261.       break;
  4262.  
  4263.     case GTU:
  4264.       val = (((unsigned HOST_WIDE_INT) arg0)
  4265.          > ((unsigned HOST_WIDE_INT) arg1) ? STORE_FLAG_VALUE : 0);
  4266.       break;
  4267.  
  4268.     default:
  4269.       abort ();
  4270.     }
  4271.  
  4272.   /* Clear the bits that don't belong in our mode, unless they and our sign
  4273.      bit are all one.  So we get either a reasonable negative value or a
  4274.      reasonable unsigned value for this mode.  */
  4275.   if (width < HOST_BITS_PER_WIDE_INT
  4276.       && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
  4277.       != ((HOST_WIDE_INT) (-1) << (width - 1))))
  4278.     val &= ((HOST_WIDE_INT) 1 << width) - 1;
  4279.   
  4280.   return GEN_INT (val);
  4281. }
  4282.  
  4283. /* Simplify CODE, an operation with result mode MODE and three operands,
  4284.    OP0, OP1, and OP2.  OP0_MODE was the mode of OP0 before it became
  4285.    a constant.  Return 0 if no simplifications is possible.  */
  4286.  
  4287. rtx
  4288. simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
  4289.      enum rtx_code code;
  4290.      enum machine_mode mode, op0_mode;
  4291.      rtx op0, op1, op2;
  4292. {
  4293.   int width = GET_MODE_BITSIZE (mode);
  4294.  
  4295.   /* VOIDmode means "infinite" precision.  */
  4296.   if (width == 0)
  4297.     width = HOST_BITS_PER_WIDE_INT;
  4298.  
  4299.   switch (code)
  4300.     {
  4301.     case SIGN_EXTRACT:
  4302.     case ZERO_EXTRACT:
  4303.       if (GET_CODE (op0) == CONST_INT
  4304.       && GET_CODE (op1) == CONST_INT
  4305.       && GET_CODE (op2) == CONST_INT
  4306.       && INTVAL (op1) + INTVAL (op2) <= GET_MODE_BITSIZE (op0_mode)
  4307.       && width <= HOST_BITS_PER_WIDE_INT)
  4308.     {
  4309.       /* Extracting a bit-field from a constant */
  4310.       HOST_WIDE_INT val = INTVAL (op0);
  4311.  
  4312. #if BITS_BIG_ENDIAN
  4313.       val >>= (GET_MODE_BITSIZE (op0_mode) - INTVAL (op2) - INTVAL (op1));
  4314. #else
  4315.       val >>= INTVAL (op2);
  4316. #endif
  4317.       if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
  4318.         {
  4319.           /* First zero-extend.  */
  4320.           val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
  4321.           /* If desired, propagate sign bit.  */
  4322.           if (code == SIGN_EXTRACT
  4323.           && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
  4324.         val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
  4325.         }
  4326.  
  4327.       /* Clear the bits that don't belong in our mode,
  4328.          unless they and our sign bit are all one.
  4329.          So we get either a reasonable negative value or a reasonable
  4330.          unsigned value for this mode.  */
  4331.       if (width < HOST_BITS_PER_WIDE_INT
  4332.           && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
  4333.           != ((HOST_WIDE_INT) (-1) << (width - 1))))
  4334.         val &= ((HOST_WIDE_INT) 1 << width) - 1;
  4335.  
  4336.       return GEN_INT (val);
  4337.     }
  4338.       break;
  4339.  
  4340.     case IF_THEN_ELSE:
  4341.       if (GET_CODE (op0) == CONST_INT)
  4342.     return op0 != const0_rtx ? op1 : op2;
  4343.       break;
  4344.  
  4345.     default:
  4346.       abort ();
  4347.     }
  4348.  
  4349.   return 0;
  4350. }
  4351.  
  4352. /* If X is a nontrivial arithmetic operation on an argument
  4353.    for which a constant value can be determined, return
  4354.    the result of operating on that value, as a constant.
  4355.    Otherwise, return X, possibly with one or more operands
  4356.    modified by recursive calls to this function.
  4357.  
  4358.    If X is a register whose contents are known, we do NOT
  4359.    return those contents.  This is because an instruction that
  4360.    uses a register is usually faster than one that uses a constant.
  4361.  
  4362.    INSN is the insn that we may be modifying.  If it is 0, make a copy
  4363.    of X before modifying it.  */
  4364.  
  4365. #ifdef MPW
  4366. #pragma segment CSE01
  4367. #endif
  4368.  
  4369. static rtx
  4370. fold_rtx (x, insn)
  4371.      rtx x;
  4372.      rtx insn;    
  4373. {
  4374.   register enum rtx_code code;
  4375.   register enum machine_mode mode;
  4376.   register char *fmt;
  4377.   register int i;
  4378.   rtx new = 0;
  4379.   int copied = 0;
  4380.   int must_swap = 0;
  4381.  
  4382.   /* Folded equivalents of first two operands of X.  */
  4383.   rtx folded_arg0;
  4384.   rtx folded_arg1;
  4385.  
  4386.   /* Constant equivalents of first three operands of X;
  4387.      0 when no such equivalent is known.  */
  4388.   rtx const_arg0;
  4389.   rtx const_arg1;
  4390.   rtx const_arg2;
  4391.  
  4392.   /* The mode of the first operand of X.  We need this for sign and zero
  4393.      extends.  */
  4394.   enum machine_mode mode_arg0;
  4395.  
  4396.   if (x == 0)
  4397.     return x;
  4398.  
  4399.   mode = GET_MODE (x);
  4400.   code = GET_CODE (x);
  4401.   switch (code)
  4402.     {
  4403.     case CONST:
  4404.     case CONST_INT:
  4405.     case CONST_DOUBLE:
  4406.     case SYMBOL_REF:
  4407.     case LABEL_REF:
  4408.     case REG:
  4409.       /* No use simplifying an EXPR_LIST
  4410.      since they are used only for lists of args
  4411.      in a function call's REG_EQUAL note.  */
  4412.     case EXPR_LIST:
  4413.       return x;
  4414.  
  4415. #ifdef HAVE_cc0
  4416.     case CC0:
  4417.       return prev_insn_cc0;
  4418. #endif
  4419.  
  4420.     case PC:
  4421.       /* If the next insn is a CODE_LABEL followed by a jump table,
  4422.      PC's value is a LABEL_REF pointing to that label.  That
  4423.      lets us fold switch statements on the Vax.  */
  4424.       if (insn && GET_CODE (insn) == JUMP_INSN)
  4425.     {
  4426.       rtx next = next_nonnote_insn (insn);
  4427.  
  4428.       if (next && GET_CODE (next) == CODE_LABEL
  4429.           && NEXT_INSN (next) != 0
  4430.           && GET_CODE (NEXT_INSN (next)) == JUMP_INSN
  4431.           && (GET_CODE (PATTERN (NEXT_INSN (next))) == ADDR_VEC
  4432.           || GET_CODE (PATTERN (NEXT_INSN (next))) == ADDR_DIFF_VEC))
  4433.         return gen_rtx (LABEL_REF, TPmode, next);
  4434.     }
  4435.       break;
  4436.  
  4437.     case SUBREG:
  4438.       /* See if we previously assigned a constant value to this SUBREG.  */
  4439.       if ((new = lookup_as_function (x, CONST_INT)) != 0
  4440.       || (new = lookup_as_function (x, CONST_DOUBLE)) != 0)
  4441.     return new;
  4442.  
  4443.       /* If this is a paradoxical SUBREG, we have no idea what value the
  4444.      extra bits would have.  However, if the operand is equivalent
  4445.      to a SUBREG whose operand is the same as our mode, and all the
  4446.      modes are within a word, we can just use the inner operand
  4447.      because these SUBREGs just say how to treat the register.  */
  4448.  
  4449.       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
  4450.     {
  4451.       enum machine_mode imode = GET_MODE (SUBREG_REG (x));
  4452.       struct table_elt *elt;
  4453.  
  4454.       if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
  4455.           && GET_MODE_SIZE (imode) <= UNITS_PER_WORD
  4456.           && (elt = lookup (SUBREG_REG (x), HASH (SUBREG_REG (x), imode),
  4457.                 imode)) != 0)
  4458.         {
  4459.           for (elt = elt->first_same_value;
  4460.            elt; elt = elt->next_same_value)
  4461.         if (GET_CODE (elt->exp) == SUBREG
  4462.             && GET_MODE (SUBREG_REG (elt->exp)) == mode
  4463.             && exp_equiv_p (elt->exp, elt->exp, 1, 0))
  4464.           return copy_rtx (SUBREG_REG (elt->exp));
  4465.         }
  4466.  
  4467.       return x;
  4468.     }
  4469.  
  4470.       /* Fold SUBREG_REG.  If it changed, see if we can simplify the SUBREG.
  4471.      We might be able to if the SUBREG is extracting a single word in an
  4472.      integral mode or extracting the low part.  */
  4473.  
  4474.       folded_arg0 = fold_rtx (SUBREG_REG (x), insn);
  4475.       const_arg0 = equiv_constant (folded_arg0);
  4476.       if (const_arg0)
  4477.     folded_arg0 = const_arg0;
  4478.  
  4479.       if (folded_arg0 != SUBREG_REG (x))
  4480.     {
  4481.       new = 0;
  4482.  
  4483.       if (GET_MODE_CLASS (mode) == MODE_INT
  4484.           && GET_MODE_SIZE (mode) == UNITS_PER_WORD
  4485.           && GET_MODE (SUBREG_REG (x)) != VOIDmode)
  4486.         new = operand_subword (folded_arg0, SUBREG_WORD (x), 0,
  4487.                    GET_MODE (SUBREG_REG (x)));
  4488.       if (new == 0 && subreg_lowpart_p (x))
  4489.         new = gen_lowpart_if_possible (mode, folded_arg0);
  4490.       if (new)
  4491.         return new;
  4492.     }
  4493.  
  4494.       /* If this is a narrowing SUBREG and our operand is a REG, see if
  4495.      we can find an equivalence for REG that is an arithmetic operation
  4496.      in a wider mode where both operands are paradoxical SUBREGs
  4497.      from objects of our result mode.  In that case, we couldn't report
  4498.      an equivalent value for that operation, since we don't know what the
  4499.      extra bits will be.  But we can find an equivalence for this SUBREG
  4500.      by folding that operation is the narrow mode.  This allows us to
  4501.      fold arithmetic in narrow modes when the machine only supports
  4502.      word-sized arithmetic.  
  4503.  
  4504.      Also look for a case where we have a SUBREG whose operand is the
  4505.      same as our result.  If both modes are smaller than a word, we
  4506.      are simply interpreting a register in different modes and we
  4507.      can use the inner value.  */
  4508.  
  4509.       if (GET_CODE (folded_arg0) == REG
  4510.       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (folded_arg0))
  4511.       && subreg_lowpart_p (x))
  4512.     {
  4513.       struct table_elt *elt;
  4514.  
  4515.       /* We can use HASH here since we know that canon_hash won't be
  4516.          called.  */
  4517.       elt = lookup (folded_arg0,
  4518.             HASH (folded_arg0, GET_MODE (folded_arg0)),
  4519.             GET_MODE (folded_arg0));
  4520.  
  4521.       if (elt)
  4522.         elt = elt->first_same_value;
  4523.  
  4524.       for (; elt; elt = elt->next_same_value)
  4525.         {
  4526.           enum rtx_code eltcode = GET_CODE (elt->exp);
  4527.  
  4528.           /* Just check for unary and binary operations.  */
  4529.           if (GET_RTX_CLASS (GET_CODE (elt->exp)) == '1'
  4530.           && GET_CODE (elt->exp) != SIGN_EXTEND
  4531.           && GET_CODE (elt->exp) != ZERO_EXTEND
  4532.           && GET_CODE (XEXP (elt->exp, 0)) == SUBREG
  4533.           && GET_MODE (SUBREG_REG (XEXP (elt->exp, 0))) == mode)
  4534.         {
  4535.           rtx op0 = SUBREG_REG (XEXP (elt->exp, 0));
  4536.  
  4537.           if (GET_CODE (op0) != REG && ! CONSTANT_P (op0))
  4538.             op0 = fold_rtx (op0, NULL_RTX);
  4539.  
  4540.           op0 = equiv_constant (op0);
  4541.           if (op0)
  4542.             new = simplify_unary_operation (GET_CODE (elt->exp), mode,
  4543.                             op0, mode);
  4544.         }
  4545.           else if ((GET_RTX_CLASS (GET_CODE (elt->exp)) == '2'
  4546.             || GET_RTX_CLASS (GET_CODE (elt->exp)) == 'c')
  4547.                && eltcode != DIV && eltcode != MOD
  4548.                && eltcode != UDIV && eltcode != UMOD
  4549.                && eltcode != ASHIFTRT && eltcode != LSHIFTRT
  4550.                && eltcode != ROTATE && eltcode != ROTATERT
  4551.                && ((GET_CODE (XEXP (elt->exp, 0)) == SUBREG
  4552.                 && (GET_MODE (SUBREG_REG (XEXP (elt->exp, 0)))
  4553.                 == mode))
  4554.                || CONSTANT_P (XEXP (elt->exp, 0)))
  4555.                && ((GET_CODE (XEXP (elt->exp, 1)) == SUBREG
  4556.                 && (GET_MODE (SUBREG_REG (XEXP (elt->exp, 1)))
  4557.                 == mode))
  4558.                || CONSTANT_P (XEXP (elt->exp, 1))))
  4559.         {
  4560.           rtx op0 = gen_lowpart_common (mode, XEXP (elt->exp, 0));
  4561.           rtx op1 = gen_lowpart_common (mode, XEXP (elt->exp, 1));
  4562.  
  4563.           if (op0 && GET_CODE (op0) != REG && ! CONSTANT_P (op0))
  4564.             op0 = fold_rtx (op0, NULL_RTX);
  4565.  
  4566.           if (op0)
  4567.             op0 = equiv_constant (op0);
  4568.  
  4569.           if (op1 && GET_CODE (op1) != REG && ! CONSTANT_P (op1))
  4570.             op1 = fold_rtx (op1, NULL_RTX);
  4571.  
  4572.           if (op1)
  4573.             op1 = equiv_constant (op1);
  4574.  
  4575.           if (op0 && op1)
  4576.             new = simplify_binary_operation (GET_CODE (elt->exp), mode,
  4577.                              op0, op1);
  4578.         }
  4579.  
  4580.           else if (GET_CODE (elt->exp) == SUBREG
  4581.                && GET_MODE (SUBREG_REG (elt->exp)) == mode
  4582.                && (GET_MODE_SIZE (GET_MODE (folded_arg0))
  4583.                <= UNITS_PER_WORD)
  4584.                && exp_equiv_p (elt->exp, elt->exp, 1, 0))
  4585.         new = copy_rtx (SUBREG_REG (elt->exp));
  4586.  
  4587.           if (new)
  4588.         return new;
  4589.         }
  4590.     }
  4591.  
  4592.       return x;
  4593.  
  4594.     case NOT:
  4595.     case NEG:
  4596.       /* If we have (NOT Y), see if Y is known to be (NOT Z).
  4597.      If so, (NOT Y) simplifies to Z.  Similarly for NEG.  */
  4598.       new = lookup_as_function (XEXP (x, 0), code);
  4599.       if (new)
  4600.     return fold_rtx (copy_rtx (XEXP (new, 0)), insn);
  4601.       break;
  4602.  
  4603.     case MEM:
  4604.       /* If we are not actually processing an insn, don't try to find the
  4605.      best address.  Not only don't we care, but we could modify the
  4606.      MEM in an invalid way since we have no insn to validate against.  */
  4607.       if (insn != 0)
  4608.     find_best_addr (insn, &XEXP (x, 0));
  4609.  
  4610.       {
  4611.     /* Even if we don't fold in the insn itself,
  4612.        we can safely do so here, in hopes of getting a constant.  */
  4613.     rtx addr = fold_rtx (XEXP (x, 0), NULL_RTX);
  4614.     rtx base = 0;
  4615.     HOST_WIDE_INT offset = 0;
  4616.  
  4617.     if (GET_CODE (addr) == REG
  4618.         && REGNO_QTY_VALID_P (REGNO (addr))
  4619.         && GET_MODE (addr) == qty_mode[reg_qty[REGNO (addr)]]
  4620.         && qty_const[reg_qty[REGNO (addr)]] != 0)
  4621.       addr = qty_const[reg_qty[REGNO (addr)]];
  4622.  
  4623.     /* If address is constant, split it into a base and integer offset.  */
  4624.     if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
  4625.       base = addr;
  4626.     else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
  4627.          && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
  4628.       {
  4629.         base = XEXP (XEXP (addr, 0), 0);
  4630.         offset = INTVAL (XEXP (XEXP (addr, 0), 1));
  4631.       }
  4632.     else if (GET_CODE (addr) == LO_SUM
  4633.          && GET_CODE (XEXP (addr, 1)) == SYMBOL_REF)
  4634.       base = XEXP (addr, 1);
  4635.  
  4636.     /* If this is a constant pool reference, we can fold it into its
  4637.        constant to allow better value tracking.  */
  4638.     if (base && GET_CODE (base) == SYMBOL_REF
  4639.         && CONSTANT_POOL_ADDRESS_P (base))
  4640.       {
  4641.         rtx constant = get_pool_constant (base);
  4642.         enum machine_mode const_mode = get_pool_mode (base);
  4643.         rtx new;
  4644.  
  4645.         if (CONSTANT_P (constant) && GET_CODE (constant) != CONST_INT)
  4646.           constant_pool_entries_cost = COST (constant);
  4647.  
  4648.         /* If we are loading the full constant, we have an equivalence.  */
  4649.         if (offset == 0 && mode == const_mode)
  4650.           return constant;
  4651.  
  4652.         /* If this actually isn't a constant (wierd!), we can't do
  4653.            anything.  Otherwise, handle the two most common cases:
  4654.            extracting a word from a multi-word constant, and extracting
  4655.            the low-order bits.  Other cases don't seem common enough to
  4656.            worry about.  */
  4657.         if (! CONSTANT_P (constant))
  4658.           return x;
  4659.  
  4660.         if (GET_MODE_CLASS (mode) == MODE_INT
  4661.         && GET_MODE_SIZE (mode) == UNITS_PER_WORD
  4662.         && offset % UNITS_PER_WORD == 0
  4663.         && (new = operand_subword (constant,
  4664.                        offset / UNITS_PER_WORD,
  4665.                        0, const_mode)) != 0)
  4666.           return new;
  4667.  
  4668.         if (((BYTES_BIG_ENDIAN
  4669.           && offset == GET_MODE_SIZE (GET_MODE (constant)) - 1)
  4670.          || (! BYTES_BIG_ENDIAN && offset == 0))
  4671.         && (new = gen_lowpart_if_possible (mode, constant)) != 0)
  4672.           return new;
  4673.       }
  4674.  
  4675.     /* If this is a reference to a label at a known position in a jump
  4676.        table, we also know its value.  */
  4677.     if (base && GET_CODE (base) == LABEL_REF)
  4678.       {
  4679.         rtx label = XEXP (base, 0);
  4680.         rtx table_insn = NEXT_INSN (label);
  4681.         
  4682.         if (table_insn && GET_CODE (table_insn) == JUMP_INSN
  4683.         && GET_CODE (PATTERN (table_insn)) == ADDR_VEC)
  4684.           {
  4685.         rtx table = PATTERN (table_insn);
  4686.  
  4687.         if (offset >= 0
  4688.             && (offset / GET_MODE_SIZE (GET_MODE (table))
  4689.             < XVECLEN (table, 0)))
  4690.           return XVECEXP (table, 0,
  4691.                   offset / GET_MODE_SIZE (GET_MODE (table)));
  4692.           }
  4693.         if (table_insn && GET_CODE (table_insn) == JUMP_INSN
  4694.         && GET_CODE (PATTERN (table_insn)) == ADDR_DIFF_VEC)
  4695.           {
  4696.         rtx table = PATTERN (table_insn);
  4697.  
  4698.         if (offset >= 0
  4699.             && (offset / GET_MODE_SIZE (GET_MODE (table))
  4700.             < XVECLEN (table, 1)))
  4701.           {
  4702.             offset /= GET_MODE_SIZE (GET_MODE (table));
  4703.             new = gen_rtx (MINUS, TPmode, XVECEXP (table, 1, offset),
  4704.                    XEXP (table, 0));
  4705.             if (GET_MODE (table) != TPmode)
  4706.               new = gen_rtx (TRUNCATE, GET_MODE (table), new);
  4707.  
  4708.  
  4709.             return new;
  4710.           }
  4711.           }
  4712.       }
  4713.  
  4714.     return x;
  4715.       }
  4716.     }
  4717.  
  4718.   const_arg0 = 0;
  4719.   const_arg1 = 0;
  4720.   const_arg2 = 0;
  4721.   mode_arg0 = VOIDmode;
  4722.  
  4723.   /* Try folding our operands.
  4724.      Then see which ones have constant values known.  */
  4725.  
  4726.   fmt = GET_RTX_FORMAT (code);
  4727.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  4728.     if (fmt[i] == 'e')
  4729.       {
  4730.     rtx arg = XEXP (x, i);
  4731.     rtx folded_arg = arg, const_arg = 0;
  4732.     enum machine_mode mode_arg = GET_MODE (arg);
  4733.     rtx cheap_arg, expensive_arg;
  4734.     rtx replacements[2];
  4735.     int j;
  4736.  
  4737.     /* Most arguments are cheap, so handle them specially.  */
  4738.     switch (GET_CODE (arg))
  4739.       {
  4740.       case REG:
  4741.         /* This is the same as calling equiv_constant; it is duplicated
  4742.            here for speed.  */
  4743.         if (REGNO_QTY_VALID_P (REGNO (arg))
  4744.         && qty_const[reg_qty[REGNO (arg)]] != 0
  4745.         && GET_CODE (qty_const[reg_qty[REGNO (arg)]]) != REG
  4746.         && GET_CODE (qty_const[reg_qty[REGNO (arg)]]) != PLUS)
  4747.           const_arg
  4748.         = gen_lowpart_if_possible (GET_MODE (arg),
  4749.                        qty_const[reg_qty[REGNO (arg)]]);
  4750.         break;
  4751.  
  4752.       case CONST:
  4753.       case CONST_INT:
  4754.       case SYMBOL_REF:
  4755.       case LABEL_REF:
  4756.       case CONST_DOUBLE:
  4757.         const_arg = arg;
  4758.         break;
  4759.  
  4760. #ifdef HAVE_cc0
  4761.       case CC0:
  4762.         folded_arg = prev_insn_cc0;
  4763.         mode_arg = prev_insn_cc0_mode;
  4764.         const_arg = equiv_constant (folded_arg);
  4765.         break;
  4766. #endif
  4767.  
  4768.       default:
  4769.         folded_arg = fold_rtx (arg, insn);
  4770.         const_arg = equiv_constant (folded_arg);
  4771.       }
  4772.  
  4773.     /* For the first three operands, see if the operand
  4774.        is constant or equivalent to a constant.  */
  4775.     switch (i)
  4776.       {
  4777.       case 0:
  4778.         folded_arg0 = folded_arg;
  4779.         const_arg0 = const_arg;
  4780.         mode_arg0 = mode_arg;
  4781.         break;
  4782.       case 1:
  4783.         folded_arg1 = folded_arg;
  4784.         const_arg1 = const_arg;
  4785.         break;
  4786.       case 2:
  4787.         const_arg2 = const_arg;
  4788.         break;
  4789.       }
  4790.  
  4791.     /* Pick the least expensive of the folded argument and an
  4792.        equivalent constant argument.  */
  4793.     if (const_arg == 0 || const_arg == folded_arg
  4794.         || COST (const_arg) > COST (folded_arg))
  4795.       cheap_arg = folded_arg, expensive_arg = const_arg;
  4796.     else
  4797.       cheap_arg = const_arg, expensive_arg = folded_arg;
  4798.  
  4799.     /* Try to replace the operand with the cheapest of the two
  4800.        possibilities.  If it doesn't work and this is either of the first
  4801.        two operands of a commutative operation, try swapping them.
  4802.        If THAT fails, try the more expensive, provided it is cheaper
  4803.        than what is already there.  */
  4804.  
  4805.     if (cheap_arg == XEXP (x, i))
  4806.       continue;
  4807.  
  4808.     if (insn == 0 && ! copied)
  4809.       {
  4810.         x = copy_rtx (x);
  4811.         copied = 1;
  4812.       }
  4813.  
  4814.     replacements[0] = cheap_arg, replacements[1] = expensive_arg;
  4815.     for (j = 0;
  4816.          j < 2 && replacements[j]
  4817.          && COST (replacements[j]) < COST (XEXP (x, i));
  4818.          j++)
  4819.       {
  4820.         if (validate_change (insn, &XEXP (x, i), replacements[j], 0))
  4821.           break;
  4822.  
  4823.         if (code == NE || code == EQ || GET_RTX_CLASS (code) == 'c')
  4824.           {
  4825.         validate_change (insn, &XEXP (x, i), XEXP (x, 1 - i), 1);
  4826.         validate_change (insn, &XEXP (x, 1 - i), replacements[j], 1);
  4827.  
  4828.         if (apply_change_group ())
  4829.           {
  4830.             /* Swap them back to be invalid so that this loop can
  4831.                continue and flag them to be swapped back later.  */
  4832.             rtx tem;
  4833.  
  4834.             tem = XEXP (x, 0); XEXP (x, 0) = XEXP (x, 1);
  4835.                        XEXP (x, 1) = tem;
  4836.             must_swap = 1;
  4837.             break;
  4838.           }
  4839.           }
  4840.       }
  4841.       }
  4842.  
  4843.     else if (fmt[i] == 'E')
  4844.       /* Don't try to fold inside of a vector of expressions.
  4845.      Doing nothing is harmless.  */
  4846.       ;
  4847.  
  4848.   /* If a commutative operation, place a constant integer as the second
  4849.      operand unless the first operand is also a constant integer.  Otherwise,
  4850.      place any constant second unless the first operand is also a constant.  */
  4851.  
  4852.   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
  4853.     {
  4854.       if (must_swap || (const_arg0
  4855.               && (const_arg1 == 0
  4856.                       || (GET_CODE (const_arg0) == CONST_INT
  4857.                     && GET_CODE (const_arg1) != CONST_INT))))
  4858.     {
  4859.       register rtx tem = XEXP (x, 0);
  4860.  
  4861.       if (insn == 0 && ! copied)
  4862.         {
  4863.           x = copy_rtx (x);
  4864.           copied = 1;
  4865.         }
  4866.  
  4867.       validate_change (insn, &XEXP (x, 0), XEXP (x, 1), 1);
  4868.       validate_change (insn, &XEXP (x, 1), tem, 1);
  4869.       if (apply_change_group ())
  4870.         {
  4871.           tem = const_arg0, const_arg0 = const_arg1, const_arg1 = tem;
  4872.           tem = folded_arg0, folded_arg0 = folded_arg1, folded_arg1 = tem;
  4873.         }
  4874.     }
  4875.     }
  4876.  
  4877.   /* If X is an arithmetic operation, see if we can simplify it.  */
  4878.  
  4879.   switch (GET_RTX_CLASS (code))
  4880.     {
  4881.     case '1':
  4882.       /* We can't simplify extension ops unless we know the original mode.  */
  4883.       if ((code == ZERO_EXTEND || code == SIGN_EXTEND)
  4884.       && mode_arg0 == VOIDmode)
  4885.     break;
  4886.       new = simplify_unary_operation (code, mode,
  4887.                       const_arg0 ? const_arg0 : folded_arg0,
  4888.                       mode_arg0);
  4889.       break;
  4890.       
  4891.     case '<':
  4892.       /* See what items are actually being compared and set FOLDED_ARG[01]
  4893.      to those values and CODE to the actual comparison code.  If any are
  4894.      constant, set CONST_ARG0 and CONST_ARG1 appropriately.  We needn't
  4895.      do anything if both operands are already known to be constant.  */
  4896.  
  4897.       if (const_arg0 == 0 || const_arg1 == 0)
  4898.     {
  4899.       struct table_elt *p0, *p1;
  4900.       rtx true = const_true_rtx, false = const0_rtx;
  4901.       enum machine_mode mode_arg1;
  4902.  
  4903. #ifdef FLOAT_STORE_FLAG_VALUE
  4904.       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  4905.         {
  4906.           true = immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, mode);
  4907.           false = CONST0_RTX (mode);
  4908.         }
  4909. #endif
  4910.  
  4911.       code = find_comparison_args (code, &folded_arg0, &folded_arg1,
  4912.                        &mode_arg0, &mode_arg1);
  4913.       const_arg0 = equiv_constant (folded_arg0);
  4914.       const_arg1 = equiv_constant (folded_arg1);
  4915.  
  4916.       /* If the mode is VOIDmode or a MODE_CC mode, we don't know
  4917.          what kinds of things are being compared, so we can't do
  4918.          anything with this comparison.  */
  4919.  
  4920.       if (mode_arg0 == VOIDmode || GET_MODE_CLASS (mode_arg0) == MODE_CC)
  4921.         break;
  4922.  
  4923.       /* If we do not now have two constants being compared, see if we
  4924.          can nevertheless deduce some things about the comparison.  */
  4925.       if (const_arg0 == 0 || const_arg1 == 0)
  4926.         {
  4927.           /* Is FOLDED_ARG0 frame-pointer plus a constant?  Or non-explicit
  4928.          constant?  These aren't zero, but we don't know their sign. */
  4929.           if (const_arg1 == const0_rtx
  4930.           && (NONZERO_BASE_PLUS_P (folded_arg0)
  4931. #if 0  /* Sad to say, on sysvr4, #pragma weak can make a symbol address
  4932.       come out as 0.  */
  4933.               || GET_CODE (folded_arg0) == SYMBOL_REF
  4934. #endif
  4935.               || GET_CODE (folded_arg0) == LABEL_REF
  4936.               || GET_CODE (folded_arg0) == CONST))
  4937.         {
  4938.           if (code == EQ)
  4939.             return false;
  4940.           else if (code == NE)
  4941.             return true;
  4942.         }
  4943.  
  4944.           /* See if the two operands are the same.  We don't do this
  4945.          for IEEE floating-point since we can't assume x == x
  4946.          since x might be a NaN.  */
  4947.  
  4948.           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  4949.            || GET_MODE_CLASS (mode_arg0) != MODE_FLOAT)
  4950.           && (folded_arg0 == folded_arg1
  4951.               || (GET_CODE (folded_arg0) == REG
  4952.               && GET_CODE (folded_arg1) == REG
  4953.               && (reg_qty[REGNO (folded_arg0)]
  4954.                   == reg_qty[REGNO (folded_arg1)]))
  4955.               || ((p0 = lookup (folded_arg0,
  4956.                     (safe_hash (folded_arg0, mode_arg0)
  4957.                      % NBUCKETS), mode_arg0))
  4958.               && (p1 = lookup (folded_arg1,
  4959.                        (safe_hash (folded_arg1, mode_arg0)
  4960.                         % NBUCKETS), mode_arg0))
  4961.               && p0->first_same_value == p1->first_same_value)))
  4962.         return ((code == EQ || code == LE || code == GE
  4963.              || code == LEU || code == GEU)
  4964.             ? true : false);
  4965.  
  4966.           /* If FOLDED_ARG0 is a register, see if the comparison we are
  4967.          doing now is either the same as we did before or the reverse
  4968.          (we only check the reverse if not floating-point).  */
  4969.           else if (GET_CODE (folded_arg0) == REG)
  4970.         {
  4971.           int qty = reg_qty[REGNO (folded_arg0)];
  4972.  
  4973.           if (REGNO_QTY_VALID_P (REGNO (folded_arg0))
  4974.               && (comparison_dominates_p (qty_comparison_code[qty], code)
  4975.               || (comparison_dominates_p (qty_comparison_code[qty],
  4976.                               reverse_condition (code))
  4977.                   && GET_MODE_CLASS (mode_arg0) == MODE_INT))
  4978.               && (rtx_equal_p (qty_comparison_const[qty], folded_arg1)
  4979.               || (const_arg1
  4980.                   && rtx_equal_p (qty_comparison_const[qty],
  4981.                           const_arg1))
  4982.               || (GET_CODE (folded_arg1) == REG
  4983.                   && (reg_qty[REGNO (folded_arg1)]
  4984.                   == qty_comparison_qty[qty]))))
  4985.             return (comparison_dominates_p (qty_comparison_code[qty],
  4986.                             code)
  4987.                 ? true : false);
  4988.         }
  4989.         }
  4990.     }
  4991.  
  4992.       /* If we are comparing against zero, see if the first operand is
  4993.      equivalent to an IOR with a constant.  If so, we may be able to
  4994.      determine the result of this comparison.  */
  4995.  
  4996.       if (const_arg1 == const0_rtx)
  4997.     {
  4998.       rtx y = lookup_as_function (folded_arg0, IOR);
  4999.       rtx inner_const;
  5000.  
  5001.       if (y != 0
  5002.           && (inner_const = equiv_constant (XEXP (y, 1))) != 0
  5003.           && GET_CODE (inner_const) == CONST_INT
  5004.           && INTVAL (inner_const) != 0)
  5005.         {
  5006.           int sign_bitnum = GET_MODE_BITSIZE (mode_arg0) - 1;
  5007.           int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
  5008.                   && (INTVAL (inner_const)
  5009.                   & ((HOST_WIDE_INT) 1 << sign_bitnum)));
  5010.           rtx true = const_true_rtx, false = const0_rtx;
  5011.  
  5012. #ifdef FLOAT_STORE_FLAG_VALUE
  5013.           if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  5014.         {
  5015.           true = immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, mode);
  5016.           false = CONST0_RTX (mode);
  5017.         }
  5018. #endif
  5019.  
  5020.           switch (code)
  5021.         {
  5022.         case EQ:
  5023.           return false;
  5024.         case NE:
  5025.           return true;
  5026.         case LT:  case LE:
  5027.           if (has_sign)
  5028.             return true;
  5029.           break;
  5030.         case GT:  case GE:
  5031.           if (has_sign)
  5032.             return false;
  5033.           break;
  5034.         }
  5035.         }
  5036.     }
  5037.  
  5038.       new = simplify_relational_operation (code, mode_arg0,
  5039.                        const_arg0 ? const_arg0 : folded_arg0,
  5040.                        const_arg1 ? const_arg1 : folded_arg1);
  5041. #ifdef FLOAT_STORE_FLAG_VALUE
  5042.       if (new != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
  5043.     new = ((new == const0_rtx) ? CONST0_RTX (mode)
  5044.            : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, mode));
  5045. #endif
  5046.       break;
  5047.  
  5048.     case '2':
  5049.     case 'c':
  5050.       switch (code)
  5051.     {
  5052.     case PLUS:
  5053.       /* If the second operand is a LABEL_REF, see if the first is a MINUS
  5054.          with that LABEL_REF as its second operand.  If so, the result is
  5055.          the first operand of that MINUS.  This handles switches with an
  5056.          ADDR_DIFF_VEC table.  */
  5057.       if (const_arg1 && GET_CODE (const_arg1) == LABEL_REF)
  5058.         {
  5059.           rtx y = lookup_as_function (folded_arg0, MINUS);
  5060.  
  5061.           if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF
  5062.           && XEXP (XEXP (y, 1), 0) == XEXP (const_arg1, 0))
  5063.         return XEXP (y, 0);
  5064.         }
  5065.       goto from_plus;
  5066.  
  5067.     case MINUS:
  5068.       /* If we have (MINUS Y C), see if Y is known to be (PLUS Z C2).
  5069.          If so, produce (PLUS Z C2-C).  */
  5070.       if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT)
  5071.         {
  5072.           rtx y = lookup_as_function (XEXP (x, 0), PLUS);
  5073.           if (y && GET_CODE (XEXP (y, 1)) == CONST_INT)
  5074.         return fold_rtx (plus_constant (y, -INTVAL (const_arg1)));
  5075.         }
  5076.  
  5077.       /* ... fall through ... */
  5078.  
  5079.     from_plus:
  5080.     case SMIN:    case SMAX:      case UMIN:    case UMAX:
  5081.     case IOR:     case AND:       case XOR:
  5082.     case MULT:    case DIV:       case UDIV:
  5083.     case ASHIFT:  case LSHIFTRT:  case ASHIFTRT:
  5084.       /* If we have (<op> <reg> <const_int>) for an associative OP and REG
  5085.          is known to be of similar form, we may be able to replace the
  5086.          operation with a combined operation.  This may eliminate the
  5087.          intermediate operation if every use is simplified in this way.
  5088.          Note that the similar optimization done by combine.c only works
  5089.          if the intermediate operation's result has only one reference.  */
  5090.  
  5091.       if (GET_CODE (folded_arg0) == REG
  5092.           && const_arg1 && GET_CODE (const_arg1) == CONST_INT)
  5093.         {
  5094.           int is_shift
  5095.         = (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT);
  5096.           rtx y = lookup_as_function (folded_arg0, code);
  5097.           rtx inner_const;
  5098.           enum rtx_code associate_code;
  5099.           rtx new_const;
  5100.  
  5101.           if (y == 0
  5102.           || 0 == (inner_const
  5103.                = equiv_constant (fold_rtx (XEXP (y, 1), 0)))
  5104.           || GET_CODE (inner_const) != CONST_INT
  5105.           /* If we have compiled a statement like
  5106.              "if (x == (x & mask1))", and now are looking at
  5107.              "x & mask2", we will have a case where the first operand
  5108.              of Y is the same as our first operand.  Unless we detect
  5109.              this case, an infinite loop will result.  */
  5110.           || XEXP (y, 0) == folded_arg0)
  5111.         break;
  5112.  
  5113.           /* Don't associate these operations if they are a PLUS with the
  5114.          same constant and it is a power of two.  These might be doable
  5115.          with a pre- or post-increment.  Similarly for two subtracts of
  5116.          identical powers of two with post decrement.  */
  5117.  
  5118.           if (code == PLUS && INTVAL (const_arg1) == INTVAL (inner_const)
  5119.           && (0
  5120. #if defined(HAVE_PRE_INCREMENT) || defined(HAVE_POST_INCREMENT)
  5121.               || exact_log2 (INTVAL (const_arg1)) >= 0
  5122. #endif
  5123. #if defined(HAVE_PRE_DECREMENT) || defined(HAVE_POST_DECREMENT)
  5124.               || exact_log2 (- INTVAL (const_arg1)) >= 0
  5125. #endif
  5126.           ))
  5127.         break;
  5128.  
  5129.           /* Compute the code used to compose the constants.  For example,
  5130.          A/C1/C2 is A/(C1 * C2), so if CODE == DIV, we want MULT.  */
  5131.  
  5132.           associate_code
  5133.         = (code == MULT || code == DIV || code == UDIV ? MULT
  5134.            : is_shift || code == PLUS || code == MINUS ? PLUS : code);
  5135.  
  5136.           new_const = simplify_binary_operation (associate_code, mode,
  5137.                              const_arg1, inner_const);
  5138.  
  5139.           if (new_const == 0)
  5140.         break;
  5141.  
  5142.           /* If we are associating shift operations, don't let this
  5143.          produce a shift of larger than the object.  This could
  5144.          occur when we following a sign-extend by a right shift on
  5145.          a machine that does a sign-extend as a pair of shifts.  */
  5146.  
  5147.           if (is_shift && GET_CODE (new_const) == CONST_INT
  5148.           && INTVAL (new_const) > GET_MODE_BITSIZE (mode))
  5149.         break;
  5150.  
  5151.           y = copy_rtx (XEXP (y, 0));
  5152.  
  5153.           /* If Y contains our first operand (the most common way this
  5154.          can happen is if Y is a MEM), we would do into an infinite
  5155.          loop if we tried to fold it.  So don't in that case.  */
  5156.  
  5157.           if (! reg_mentioned_p (folded_arg0, y))
  5158.         y = fold_rtx (y, insn);
  5159.  
  5160.           new = simplify_binary_operation (code, mode, y, new_const);
  5161.           if (new)
  5162.         return new;
  5163.  
  5164.           return gen_rtx (code, mode, y, new_const);
  5165.         }
  5166.     }
  5167.  
  5168.       new = simplify_binary_operation (code, mode,
  5169.                        const_arg0 ? const_arg0 : folded_arg0,
  5170.                        const_arg1 ? const_arg1 : folded_arg1);
  5171.       break;
  5172.  
  5173.     case 'o':
  5174.       /* (lo_sum (high X) X) is simply X.  */
  5175.       if (code == LO_SUM && const_arg0 != 0
  5176.       && GET_CODE (const_arg0) == HIGH
  5177.       && rtx_equal_p (XEXP (const_arg0, 0), const_arg1))
  5178.     return const_arg1;
  5179.       break;
  5180.  
  5181.     case '3':
  5182.     case 'b':
  5183.       new = simplify_ternary_operation (code, mode, mode_arg0,
  5184.                     const_arg0 ? const_arg0 : folded_arg0,
  5185.                     const_arg1 ? const_arg1 : folded_arg1,
  5186.                     const_arg2 ? const_arg2 : XEXP (x, 2));
  5187.       break;
  5188.     }
  5189.  
  5190.   return new ? new : x;
  5191. }
  5192.  
  5193. /* Return a constant value currently equivalent to X.
  5194.    Return 0 if we don't know one.  */
  5195.  
  5196. static rtx
  5197. equiv_constant (x)
  5198.      rtx x;
  5199. {
  5200.   if (GET_CODE (x) == REG
  5201.       && REGNO_QTY_VALID_P (REGNO (x))
  5202.       && qty_const[reg_qty[REGNO (x)]])
  5203.     x = gen_lowpart_if_possible (GET_MODE (x), qty_const[reg_qty[REGNO (x)]]);
  5204.  
  5205.   if (x != 0 && CONSTANT_P (x))
  5206.     return x;
  5207.  
  5208.   /* If X is a MEM, try to fold it outside the context of any insn to see if
  5209.      it might be equivalent to a constant.  That handles the case where it
  5210.      is a constant-pool reference.  Then try to look it up in the hash table
  5211.      in case it is something whose value we have seen before.  */
  5212.  
  5213.   if (GET_CODE (x) == MEM)
  5214.     {
  5215.       struct table_elt *elt;
  5216.  
  5217.       x = fold_rtx (x, NULL_RTX);
  5218.       if (CONSTANT_P (x))
  5219.     return x;
  5220.  
  5221.       elt = lookup (x, safe_hash (x, GET_MODE (x)) % NBUCKETS, GET_MODE (x));
  5222.       if (elt == 0)
  5223.     return 0;
  5224.  
  5225.       for (elt = elt->first_same_value; elt; elt = elt->next_same_value)
  5226.     if (elt->is_const && CONSTANT_P (elt->exp))
  5227.       return elt->exp;
  5228.     }
  5229.  
  5230.   return 0;
  5231. }
  5232.  
  5233. /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a fixed-point
  5234.    number, return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
  5235.    least-significant part of X.
  5236.    MODE specifies how big a part of X to return.  
  5237.  
  5238.    If the requested operation cannot be done, 0 is returned.
  5239.  
  5240.    This is similar to gen_lowpart in emit-rtl.c.  */
  5241.  
  5242. rtx
  5243. gen_lowpart_if_possible (mode, x)
  5244.      enum machine_mode mode;
  5245.      register rtx x;
  5246. {
  5247.   rtx result = gen_lowpart_common (mode, x);
  5248.  
  5249.   if (result)
  5250.     return result;
  5251.   else if (GET_CODE (x) == MEM)
  5252.     {
  5253.       /* This is the only other case we handle.  */
  5254.       register int offset = 0;
  5255.       rtx new;
  5256.  
  5257. #if WORDS_BIG_ENDIAN
  5258.       offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
  5259.         - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
  5260. #endif
  5261. #if BYTES_BIG_ENDIAN
  5262.       /* Adjust the address so that the address-after-the-data
  5263.      is unchanged.  */
  5264.       offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
  5265.          - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
  5266. #endif
  5267.       new = gen_rtx (MEM, mode, plus_constant (XEXP (x, 0), offset));
  5268.       if (! memory_address_p (mode, XEXP (new, 0)))
  5269.     return 0;
  5270.       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
  5271.       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
  5272.       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
  5273.       return new;
  5274.     }
  5275.   else
  5276.     return 0;
  5277. }
  5278.  
  5279. /* Given INSN, a jump insn, TAKEN indicates if we are following the "taken"
  5280.    branch.  It will be zero if not.
  5281.  
  5282.    In certain cases, this can cause us to add an equivalence.  For example,
  5283.    if we are following the taken case of 
  5284.        if (i == 2)
  5285.    we can add the fact that `i' and '2' are now equivalent.
  5286.  
  5287.    In any case, we can record that this comparison was passed.  If the same
  5288.    comparison is seen later, we will know its value.  */
  5289.  
  5290. static void
  5291. record_jump_equiv (insn, taken)
  5292.      rtx insn;
  5293.      int taken;
  5294. {
  5295.   int cond_known_true;
  5296.   rtx op0, op1;
  5297.   enum machine_mode mode, mode0, mode1;
  5298.   int reversed_nonequality = 0;
  5299.   enum rtx_code code;
  5300.  
  5301.   /* Ensure this is the right kind of insn.  */
  5302.   if (! condjump_p (insn) || simplejump_p (insn))
  5303.     return;
  5304.  
  5305.   /* See if this jump condition is known true or false.  */
  5306.   if (taken)
  5307.     cond_known_true = (XEXP (SET_SRC (PATTERN (insn)), 2) == pc_rtx);
  5308.   else
  5309.     cond_known_true = (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx);
  5310.  
  5311.   /* Get the type of comparison being done and the operands being compared.
  5312.      If we had to reverse a non-equality condition, record that fact so we
  5313.      know that it isn't valid for floating-point.  */
  5314.   code = GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 0));
  5315.   op0 = fold_rtx (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0), insn);
  5316.   op1 = fold_rtx (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 1), insn);
  5317.  
  5318.   code = find_comparison_args (code, &op0, &op1, &mode0, &mode1);
  5319.   if (! cond_known_true)
  5320.     {
  5321.       reversed_nonequality = (code != EQ && code != NE);
  5322.       code = reverse_condition (code);
  5323.     }
  5324.  
  5325.   /* The mode is the mode of the non-constant.  */
  5326.   mode = mode0;
  5327.   if (mode1 != VOIDmode)
  5328.     mode = mode1;
  5329.  
  5330.   record_jump_cond (code, mode, op0, op1, reversed_nonequality);
  5331. }
  5332.  
  5333. /* We know that comparison CODE applied to OP0 and OP1 in MODE is true.
  5334.    REVERSED_NONEQUALITY is nonzero if CODE had to be swapped.
  5335.    Make any useful entries we can with that information.  Called from
  5336.    above function and called recursively.  */
  5337.  
  5338. static void
  5339. record_jump_cond (code, mode, op0, op1, reversed_nonequality)
  5340.      enum rtx_code code;
  5341.      enum machine_mode mode;
  5342.      rtx op0, op1;
  5343.      int reversed_nonequality;
  5344. {
  5345.   int op0_hash_code, op1_hash_code;
  5346.   int op0_in_memory, op0_in_struct, op1_in_memory, op1_in_struct;
  5347.   struct table_elt *op0_elt, *op1_elt;
  5348.  
  5349.   /* If OP0 and OP1 are known equal, and either is a paradoxical SUBREG,
  5350.      we know that they are also equal in the smaller mode (this is also
  5351.      true for all smaller modes whether or not there is a SUBREG, but
  5352.      is not worth testing for with no SUBREG.  */
  5353.  
  5354.   if (code == EQ && GET_CODE (op0) == SUBREG
  5355.       && GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
  5356.     {
  5357.       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
  5358.       rtx tem = gen_lowpart_if_possible (inner_mode, op1);
  5359.  
  5360.       record_jump_cond (code, mode, SUBREG_REG (op0),
  5361.             tem ? tem : gen_rtx (SUBREG, inner_mode, op1, 0),
  5362.             reversed_nonequality);
  5363.     }
  5364.  
  5365.   if (code == EQ && GET_CODE (op1) == SUBREG
  5366.       && GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1))))
  5367.     {
  5368.       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
  5369.       rtx tem = gen_lowpart_if_possible (inner_mode, op0);
  5370.  
  5371.       record_jump_cond (code, mode, SUBREG_REG (op1),
  5372.             tem ? tem : gen_rtx (SUBREG, inner_mode, op0, 0),
  5373.             reversed_nonequality);
  5374.     }
  5375.  
  5376.   /* Similarly, if this is an NE comparison, and either is a SUBREG 
  5377.      making a smaller mode, we know the whole thing is also NE.  */
  5378.  
  5379.   if (code == NE && GET_CODE (op0) == SUBREG
  5380.       && subreg_lowpart_p (op0)
  5381.       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
  5382.     {
  5383.       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
  5384.       rtx tem = gen_lowpart_if_possible (inner_mode, op1);
  5385.  
  5386.       record_jump_cond (code, mode, SUBREG_REG (op0),
  5387.             tem ? tem : gen_rtx (SUBREG, inner_mode, op1, 0),
  5388.             reversed_nonequality);
  5389.     }
  5390.  
  5391.   if (code == NE && GET_CODE (op1) == SUBREG
  5392.       && subreg_lowpart_p (op1)
  5393.       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1))))
  5394.     {
  5395.       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
  5396.       rtx tem = gen_lowpart_if_possible (inner_mode, op0);
  5397.  
  5398.       record_jump_cond (code, mode, SUBREG_REG (op1),
  5399.             tem ? tem : gen_rtx (SUBREG, inner_mode, op0, 0),
  5400.             reversed_nonequality);
  5401.     }
  5402.  
  5403.   /* Hash both operands.  */
  5404.  
  5405.   do_not_record = 0;
  5406.   hash_arg_in_memory = 0;
  5407.   hash_arg_in_struct = 0;
  5408.   op0_hash_code = HASH (op0, mode);
  5409.   op0_in_memory = hash_arg_in_memory;
  5410.   op0_in_struct = hash_arg_in_struct;
  5411.  
  5412.   if (do_not_record)
  5413.     return;
  5414.  
  5415.   do_not_record = 0;
  5416.   hash_arg_in_memory = 0;
  5417.   hash_arg_in_struct = 0;
  5418.   op1_hash_code = HASH (op1, mode);
  5419.   op1_in_memory = hash_arg_in_memory;
  5420.   op1_in_struct = hash_arg_in_struct;
  5421.   
  5422.   if (do_not_record)
  5423.     return;
  5424.  
  5425.   /* Look up both operands.  */
  5426.   op0_elt = lookup (op0, op0_hash_code, mode);
  5427.   op1_elt = lookup (op1, op1_hash_code, mode);
  5428.  
  5429.   /* If we aren't setting two things equal all we can do is save this
  5430.      comparison.   Similarly if this is floating-point.  In the latter
  5431.      case, OP1 might be zero and both -0.0 and 0.0 are equal to it.
  5432.      If we record the equality, we might inadvertently delete code
  5433.      whose intent was to change -0 to +0.  */
  5434.  
  5435.   if (code != EQ || GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
  5436.     {
  5437.       /* If we reversed a floating-point comparison, if OP0 is not a
  5438.      register, or if OP1 is neither a register or constant, we can't
  5439.      do anything.  */
  5440.  
  5441.       if (GET_CODE (op1) != REG)
  5442.     op1 = equiv_constant (op1);
  5443.  
  5444.       if ((reversed_nonequality && GET_MODE_CLASS (mode) != MODE_INT)
  5445.       || GET_CODE (op0) != REG || op1 == 0)
  5446.     return;
  5447.  
  5448.       /* Put OP0 in the hash table if it isn't already.  This gives it a
  5449.      new quantity number.  */
  5450.       if (op0_elt == 0)
  5451.     {
  5452.       if (insert_regs (op0, NULL_PTR, 0))
  5453.         {
  5454.           rehash_using_reg (op0);
  5455.           op0_hash_code = HASH (op0, mode);
  5456.         }
  5457.  
  5458.       op0_elt = insert (op0, NULL_PTR, op0_hash_code, mode);
  5459.       op0_elt->in_memory = op0_in_memory;
  5460.       op0_elt->in_struct = op0_in_struct;
  5461.     }
  5462.  
  5463. #ifdef MPW_C
  5464. {
  5465. int t1 = reg_qty[REGNO (op0)];
  5466.       qty_comparison_code[t1] = code;
  5467.       if (GET_CODE (op1) == REG)
  5468.     {
  5469.       /* Put OP1 in the hash table so it gets a new quantity number.  */
  5470.       if (op1_elt == 0)
  5471.         {
  5472.           if (insert_regs (op1, 0, 0))
  5473.         {
  5474.           rehash_using_reg (op1);
  5475.           op1_hash_code = HASH (op1, mode);
  5476.         }
  5477.  
  5478.           op1_elt = insert (op1, 0, op1_hash_code, mode);
  5479.           op1_elt->in_memory = op1_in_memory;
  5480.           op1_elt->in_struct = op1_in_struct;
  5481.         }
  5482.       
  5483.       t1 = reg_qty[REGNO (op0)];
  5484.       qty_comparison_qty[t1] = reg_qty[REGNO (op1)];
  5485.       t1 = reg_qty[REGNO (op0)];
  5486.       qty_comparison_const[t1] = 0;
  5487.     }
  5488.       else
  5489.     {
  5490.       t1 = reg_qty[REGNO (op0)];
  5491.       qty_comparison_qty[t1] = -1;
  5492.       t1 = reg_qty[REGNO (op0)];
  5493.       qty_comparison_const[t1] = op1;
  5494.     }
  5495. }
  5496. #else
  5497.       qty_comparison_code[reg_qty[REGNO (op0)]] = code;
  5498.       if (GET_CODE (op1) == REG)
  5499.     {
  5500.       /* Put OP1 in the hash table so it gets a new quantity number.  */
  5501.       if (op1_elt == 0)
  5502.         {
  5503.           if (insert_regs (op1, NULL_PTR, 0))
  5504.         {
  5505.           rehash_using_reg (op1);
  5506.           op1_hash_code = HASH (op1, mode);
  5507.         }
  5508.  
  5509.           op1_elt = insert (op1, NULL_PTR, op1_hash_code, mode);
  5510.           op1_elt->in_memory = op1_in_memory;
  5511.           op1_elt->in_struct = op1_in_struct;
  5512.         }
  5513.  
  5514.       qty_comparison_qty[reg_qty[REGNO (op0)]] = reg_qty[REGNO (op1)];
  5515.       qty_comparison_const[reg_qty[REGNO (op0)]] = 0;
  5516.     }
  5517.       else
  5518.     {
  5519.       qty_comparison_qty[reg_qty[REGNO (op0)]] = -1;
  5520.       qty_comparison_const[reg_qty[REGNO (op0)]] = op1;
  5521.     }
  5522. #endif
  5523.       return;
  5524.     }
  5525.  
  5526.   /* If both are equivalent, merge the two classes.  Save this class for
  5527.      `cse_set_around_loop'.  */
  5528.   if (op0_elt && op1_elt)
  5529.     {
  5530.       merge_equiv_classes (op0_elt, op1_elt);
  5531.       last_jump_equiv_class = op0_elt;
  5532.     }
  5533.  
  5534.   /* For whichever side doesn't have an equivalence, make one.  */
  5535.   if (op0_elt == 0)
  5536.     {
  5537.       if (insert_regs (op0, op1_elt, 0))
  5538.     {
  5539.       rehash_using_reg (op0);
  5540.       op0_hash_code = HASH (op0, mode);
  5541.     }
  5542.  
  5543.       op0_elt = insert (op0, op1_elt, op0_hash_code, mode);
  5544.       op0_elt->in_memory = op0_in_memory;
  5545.       op0_elt->in_struct = op0_in_struct;
  5546.       last_jump_equiv_class = op0_elt;
  5547.     }
  5548.  
  5549.   if (op1_elt == 0)
  5550.     {
  5551.       if (insert_regs (op1, op0_elt, 0))
  5552.     {
  5553.       rehash_using_reg (op1);
  5554.       op1_hash_code = HASH (op1, mode);
  5555.     }
  5556.  
  5557.       op1_elt = insert (op1, op0_elt, op1_hash_code, mode);
  5558.       op1_elt->in_memory = op1_in_memory;
  5559.       op1_elt->in_struct = op1_in_struct;
  5560.       last_jump_equiv_class = op1_elt;
  5561.     }
  5562. }
  5563.  
  5564. /* CSE processing for one instruction.
  5565.    First simplify sources and addresses of all assignments
  5566.    in the instruction, using previously-computed equivalents values.
  5567.    Then install the new sources and destinations in the table
  5568.    of available values. 
  5569.  
  5570.    If IN_LIBCALL_BLOCK is nonzero, don't record any equivalence made in
  5571.    the insn.  */
  5572.  
  5573. /* Data on one SET contained in the instruction.  */
  5574.  
  5575. struct set
  5576. {
  5577.   /* The SET rtx itself.  */
  5578.   rtx rtl;
  5579.   /* The SET_SRC of the rtx (the original value, if it is changing).  */
  5580.   rtx src;
  5581.   /* The hash-table element for the SET_SRC of the SET.  */
  5582.   struct table_elt *src_elt;
  5583.   /* Hash code for the SET_SRC.  */
  5584.   int src_hash_code;
  5585.   /* Hash code for the SET_DEST.  */
  5586.   int dest_hash_code;
  5587.   /* The SET_DEST, with SUBREG, etc., stripped.  */
  5588.   rtx inner_dest;
  5589.   /* Place where the pointer to the INNER_DEST was found.  */
  5590.   rtx *inner_dest_loc;
  5591.   /* Nonzero if the SET_SRC is in memory.  */ 
  5592.   char src_in_memory;
  5593.   /* Nonzero if the SET_SRC is in a structure.  */ 
  5594.   char src_in_struct;
  5595.   /* Nonzero if the SET_SRC contains something
  5596.      whose value cannot be predicted and understood.  */
  5597.   char src_volatile;
  5598.   /* Original machine mode, in case it becomes a CONST_INT.  */
  5599.   enum machine_mode mode;
  5600.   /* A constant equivalent for SET_SRC, if any.  */
  5601.   rtx src_const;
  5602.   /* Hash code of constant equivalent for SET_SRC.  */
  5603.   int src_const_hash_code;
  5604.   /* Table entry for constant equivalent for SET_SRC, if any.  */
  5605.   struct table_elt *src_const_elt;
  5606. };
  5607.  
  5608. #ifdef MPW
  5609. #pragma segment CSE02
  5610. #endif
  5611.  
  5612. static void
  5613. cse_insn (insn, in_libcall_block)
  5614.      rtx insn;
  5615.      int in_libcall_block;
  5616. {
  5617.   register rtx x = PATTERN (insn);
  5618.   rtx tem;
  5619.   register int i;
  5620.   register int n_sets = 0;
  5621.  
  5622.   /* Records what this insn does to set CC0.  */
  5623.   rtx this_insn_cc0 = 0;
  5624.   enum machine_mode this_insn_cc0_mode;
  5625.   struct write_data writes_memory;
  5626.   static struct write_data init = {0, 0, 0, 0};
  5627.  
  5628.   rtx src_eqv = 0;
  5629.   struct table_elt *src_eqv_elt = 0;
  5630.   int src_eqv_volatile;
  5631.   int src_eqv_in_memory;
  5632.   int src_eqv_in_struct;
  5633.   int src_eqv_hash_code;
  5634.  
  5635.   struct set *sets;
  5636.  
  5637.   this_insn = insn;
  5638.   writes_memory = init;
  5639.  
  5640.   /* Find all the SETs and CLOBBERs in this instruction.
  5641.      Record all the SETs in the array `set' and count them.
  5642.      Also determine whether there is a CLOBBER that invalidates
  5643.      all memory references, or all references at varying addresses.  */
  5644.  
  5645.   if (GET_CODE (x) == SET)
  5646.     {
  5647.       sets = (struct set *) alloca (sizeof (struct set));
  5648.       sets[0].rtl = x;
  5649.  
  5650.       /* Ignore SETs that are unconditional jumps.
  5651.      They never need cse processing, so this does not hurt.
  5652.      The reason is not efficiency but rather
  5653.      so that we can test at the end for instructions
  5654.      that have been simplified to unconditional jumps
  5655.      and not be misled by unchanged instructions
  5656.      that were unconditional jumps to begin with.  */
  5657.       if (SET_DEST (x) == pc_rtx
  5658.       && GET_CODE (SET_SRC (x)) == LABEL_REF)
  5659.     ;
  5660.  
  5661.       /* Don't count call-insns, (set (reg 0) (call ...)), as a set.
  5662.      The hard function value register is used only once, to copy to
  5663.      someplace else, so it isn't worth cse'ing (and on 80386 is unsafe)!
  5664.      Ensure we invalidate the destination register.  On the 80386 no
  5665.      other code would invalidate it since it is a fixed_reg.
  5666.      We need not check the return of apply_change_group; see canon_reg. */
  5667.  
  5668.       else if (GET_CODE (SET_SRC (x)) == CALL)
  5669.     {
  5670.       canon_reg (SET_SRC (x), insn);
  5671.       apply_change_group ();
  5672.       fold_rtx (SET_SRC (x), insn);
  5673.       invalidate (SET_DEST (x));
  5674.     }
  5675.       else
  5676.     n_sets = 1;
  5677.     }
  5678.   else if (GET_CODE (x) == PARALLEL)
  5679.     {
  5680.       register int lim = XVECLEN (x, 0);
  5681.  
  5682.       sets = (struct set *) alloca (lim * sizeof (struct set));
  5683.  
  5684.       /* Find all regs explicitly clobbered in this insn,
  5685.      and ensure they are not replaced with any other regs
  5686.      elsewhere in this insn.
  5687.      When a reg that is clobbered is also used for input,
  5688.      we should presume that that is for a reason,
  5689.      and we should not substitute some other register
  5690.      which is not supposed to be clobbered.
  5691.      Therefore, this loop cannot be merged into the one below
  5692.      because a CALL may precede a CLOBBER and refer to the
  5693.      value clobbered.  We must not let a canonicalization do
  5694.      anything in that case.  */
  5695.       for (i = 0; i < lim; i++)
  5696.     {
  5697.       register rtx y = XVECEXP (x, 0, i);
  5698.       if (GET_CODE (y) == CLOBBER
  5699.           && (GET_CODE (XEXP (y, 0)) == REG
  5700.           || GET_CODE (XEXP (y, 0)) == SUBREG))
  5701.         invalidate (XEXP (y, 0));
  5702.     }
  5703.         
  5704.       for (i = 0; i < lim; i++)
  5705.     {
  5706.       register rtx y = XVECEXP (x, 0, i);
  5707.       if (GET_CODE (y) == SET)
  5708.         {
  5709.           /* As above, we ignore unconditional jumps and call-insns and
  5710.          ignore the result of apply_change_group.  */
  5711.           if (GET_CODE (SET_SRC (y)) == CALL)
  5712.         {
  5713.           canon_reg (SET_SRC (y), insn);
  5714.           apply_change_group ();
  5715.           fold_rtx (SET_SRC (y), insn);
  5716.           invalidate (SET_DEST (y));
  5717.         }
  5718.           else if (SET_DEST (y) == pc_rtx
  5719.                && GET_CODE (SET_SRC (y)) == LABEL_REF)
  5720.         ;
  5721.           else
  5722.         sets[n_sets++].rtl = y;
  5723.         }
  5724.       else if (GET_CODE (y) == CLOBBER)
  5725.         {
  5726.           /* If we clobber memory, take note of that,
  5727.          and canon the address.
  5728.          This does nothing when a register is clobbered
  5729.          because we have already invalidated the reg.  */
  5730.           if (GET_CODE (XEXP (y, 0)) == MEM)
  5731.         {
  5732.           canon_reg (XEXP (y, 0), NULL_RTX);
  5733.           note_mem_written (XEXP (y, 0), &writes_memory);
  5734.         }
  5735.         }
  5736.       else if (GET_CODE (y) == USE
  5737.            && ! (GET_CODE (XEXP (y, 0)) == REG
  5738.              && REGNO (XEXP (y, 0)) < FIRST_PSEUDO_REGISTER))
  5739.         canon_reg (y, NULL_RTX);
  5740.       else if (GET_CODE (y) == CALL)
  5741.         {
  5742.           /* The result of apply_change_group can be ignored; see
  5743.          canon_reg.  */
  5744.           canon_reg (y, insn);
  5745.           apply_change_group ();
  5746.           fold_rtx (y, insn);
  5747.         }
  5748.     }
  5749.     }
  5750.   else if (GET_CODE (x) == CLOBBER)
  5751.     {
  5752.       if (GET_CODE (XEXP (x, 0)) == MEM)
  5753.     {
  5754.       canon_reg (XEXP (x, 0), NULL_RTX);
  5755.       note_mem_written (XEXP (x, 0), &writes_memory);
  5756.     }
  5757.     }
  5758.  
  5759.   /* Canonicalize a USE of a pseudo register or memory location.  */
  5760.   else if (GET_CODE (x) == USE
  5761.        && ! (GET_CODE (XEXP (x, 0)) == REG
  5762.          && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER))
  5763.     canon_reg (XEXP (x, 0), NULL_RTX);
  5764.   else if (GET_CODE (x) == CALL)
  5765.     {
  5766.       /* The result of apply_change_group can be ignored; see canon_reg.  */
  5767.       canon_reg (x, insn);
  5768.       apply_change_group ();
  5769.       fold_rtx (x, insn);
  5770.     }
  5771.  
  5772.   if (n_sets == 1 && REG_NOTES (insn) != 0)
  5773.     {
  5774.       /* Store the equivalent value in SRC_EQV, if different.  */
  5775.       rtx tem = find_reg_note (insn, REG_EQUAL, NULL_RTX);
  5776.  
  5777.       if (tem && ! rtx_equal_p (XEXP (tem, 0), SET_SRC (sets[0].rtl)))
  5778.         src_eqv = canon_reg (XEXP (tem, 0), NULL_RTX);
  5779.     }
  5780.  
  5781.   /* Canonicalize sources and addresses of destinations.
  5782.      We do this in a separate pass to avoid problems when a MATCH_DUP is
  5783.      present in the insn pattern.  In that case, we want to ensure that
  5784.      we don't break the duplicate nature of the pattern.  So we will replace
  5785.      both operands at the same time.  Otherwise, we would fail to find an
  5786.      equivalent substitution in the loop calling validate_change below.
  5787.  
  5788.      We used to suppress canonicalization of DEST if it appears in SRC,
  5789.      but we don't do this any more.  */
  5790.  
  5791.   for (i = 0; i < n_sets; i++)
  5792.     {
  5793.       rtx dest = SET_DEST (sets[i].rtl);
  5794.       rtx src = SET_SRC (sets[i].rtl);
  5795.       rtx new = canon_reg (src, insn);
  5796.  
  5797.       if ((GET_CODE (new) == REG && GET_CODE (src) == REG
  5798.        && ((REGNO (new) < FIRST_PSEUDO_REGISTER)
  5799.            != (REGNO (src) < FIRST_PSEUDO_REGISTER)))
  5800.       || insn_n_dups[recog_memoized (insn)] > 0)
  5801.     validate_change (insn, &SET_SRC (sets[i].rtl), new, 1);
  5802.       else
  5803.     SET_SRC (sets[i].rtl) = new;
  5804.  
  5805.       if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
  5806.     {
  5807.       validate_change (insn, &XEXP (dest, 1),
  5808.                canon_reg (XEXP (dest, 1), insn), 1);
  5809.       validate_change (insn, &XEXP (dest, 2),
  5810.                canon_reg (XEXP (dest, 2), insn), 1);
  5811.     }
  5812.  
  5813.       while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
  5814.          || GET_CODE (dest) == ZERO_EXTRACT
  5815.          || GET_CODE (dest) == SIGN_EXTRACT)
  5816.     dest = XEXP (dest, 0);
  5817.  
  5818.       if (GET_CODE (dest) == MEM)
  5819.     canon_reg (dest, insn);
  5820.     }
  5821.  
  5822.   /* Now that we have done all the replacements, we can apply the change
  5823.      group and see if they all work.  Note that this will cause some
  5824.      canonicalizations that would have worked individually not to be applied
  5825.      because some other canonicalization didn't work, but this should not
  5826.      occur often. 
  5827.  
  5828.      The result of apply_change_group can be ignored; see canon_reg.  */
  5829.  
  5830.   apply_change_group ();
  5831.  
  5832.   /* Set sets[i].src_elt to the class each source belongs to.
  5833.      Detect assignments from or to volatile things
  5834.      and set set[i] to zero so they will be ignored
  5835.      in the rest of this function.
  5836.  
  5837.      Nothing in this loop changes the hash table or the register chains.  */
  5838.  
  5839.   for (i = 0; i < n_sets; i++)
  5840.     {
  5841.       register rtx src, dest;
  5842.       register rtx src_folded;
  5843.       register struct table_elt *elt = 0, *p;
  5844.       enum machine_mode mode;
  5845.       rtx src_eqv_here;
  5846.       rtx src_const = 0;
  5847.       rtx src_related = 0;
  5848.       struct table_elt *src_const_elt = 0;
  5849.       int src_cost = 10000, src_eqv_cost = 10000, src_folded_cost = 10000;
  5850.       int src_related_cost = 10000, src_elt_cost = 10000;
  5851.       /* Set non-zero if we need to call force_const_mem on with the
  5852.      contents of src_folded before using it.  */
  5853.       int src_folded_force_flag = 0;
  5854.  
  5855.       dest = SET_DEST (sets[i].rtl);
  5856.       src = SET_SRC (sets[i].rtl);
  5857.  
  5858.       /* If SRC is a constant that has no machine mode,
  5859.      hash it with the destination's machine mode.
  5860.      This way we can keep different modes separate.  */
  5861.  
  5862.       mode = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
  5863.       sets[i].mode = mode;
  5864.  
  5865.       if (src_eqv)
  5866.     {
  5867.       enum machine_mode eqvmode = mode;
  5868.       if (GET_CODE (dest) == STRICT_LOW_PART)
  5869.         eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
  5870.       do_not_record = 0;
  5871.       hash_arg_in_memory = 0;
  5872.       hash_arg_in_struct = 0;
  5873.       src_eqv = fold_rtx (src_eqv, insn);
  5874.       src_eqv_hash_code = HASH (src_eqv, eqvmode);
  5875.  
  5876.       /* Find the equivalence class for the equivalent expression.  */
  5877.  
  5878.       if (!do_not_record)
  5879.         src_eqv_elt = lookup (src_eqv, src_eqv_hash_code, eqvmode);
  5880.  
  5881.       src_eqv_volatile = do_not_record;
  5882.       src_eqv_in_memory = hash_arg_in_memory;
  5883.       src_eqv_in_struct = hash_arg_in_struct;
  5884.     }
  5885.  
  5886.       /* If this is a STRICT_LOW_PART assignment, src_eqv corresponds to the
  5887.      value of the INNER register, not the destination.  So it is not
  5888.      a legal substitution for the source.  But save it for later.  */
  5889.       if (GET_CODE (dest) == STRICT_LOW_PART)
  5890.     src_eqv_here = 0;
  5891.       else
  5892.     src_eqv_here = src_eqv;
  5893.  
  5894.       /* Simplify and foldable subexpressions in SRC.  Then get the fully-
  5895.      simplified result, which may not necessarily be valid.  */
  5896.       src_folded = fold_rtx (src, insn);
  5897.  
  5898.       /* If storing a constant in a bitfield, pre-truncate the constant
  5899.      so we will be able to record it later.  */
  5900.       if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT
  5901.       || GET_CODE (SET_DEST (sets[i].rtl)) == SIGN_EXTRACT)
  5902.     {
  5903.       rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
  5904.  
  5905.       if (GET_CODE (src) == CONST_INT
  5906.           && GET_CODE (width) == CONST_INT
  5907.           && INTVAL (width) < HOST_BITS_PER_WIDE_INT
  5908.           && (INTVAL (src) & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
  5909.         src_folded
  5910.           = GEN_INT (INTVAL (src) & (((HOST_WIDE_INT) 1
  5911.                       << INTVAL (width)) - 1));
  5912.     }
  5913.  
  5914.       /* Compute SRC's hash code, and also notice if it
  5915.      should not be recorded at all.  In that case,
  5916.      prevent any further processing of this assignment.  */
  5917.       do_not_record = 0;
  5918.       hash_arg_in_memory = 0;
  5919.       hash_arg_in_struct = 0;
  5920.  
  5921.       sets[i].src = src;
  5922.       sets[i].src_hash_code = HASH (src, mode);
  5923.       sets[i].src_volatile = do_not_record;
  5924.       sets[i].src_in_memory = hash_arg_in_memory;
  5925.       sets[i].src_in_struct = hash_arg_in_struct;
  5926.  
  5927. #if 0
  5928.       /* It is no longer clear why we used to do this, but it doesn't
  5929.      appear to still be needed.  So let's try without it since this
  5930.      code hurts cse'ing widened ops.  */
  5931.       /* If source is a perverse subreg (such as QI treated as an SI),
  5932.      treat it as volatile.  It may do the work of an SI in one context
  5933.      where the extra bits are not being used, but cannot replace an SI
  5934.      in general.  */
  5935.       if (GET_CODE (src) == SUBREG
  5936.       && (GET_MODE_SIZE (GET_MODE (src))
  5937.           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
  5938.     sets[i].src_volatile = 1;
  5939. #endif
  5940.  
  5941.       /* Locate all possible equivalent forms for SRC.  Try to replace
  5942.          SRC in the insn with each cheaper equivalent.
  5943.  
  5944.          We have the following types of equivalents: SRC itself, a folded
  5945.          version, a value given in a REG_EQUAL note, or a value related
  5946.      to a constant.
  5947.  
  5948.          Each of these equivalents may be part of an additional class
  5949.          of equivalents (if more than one is in the table, they must be in
  5950.          the same class; we check for this).
  5951.  
  5952.      If the source is volatile, we don't do any table lookups.
  5953.  
  5954.          We note any constant equivalent for possible later use in a
  5955.          REG_NOTE.  */
  5956.  
  5957.       if (!sets[i].src_volatile)
  5958.     elt = lookup (src, sets[i].src_hash_code, mode);
  5959.  
  5960.       sets[i].src_elt = elt;
  5961.  
  5962.       if (elt && src_eqv_here && src_eqv_elt)
  5963.         {
  5964.           if (elt->first_same_value != src_eqv_elt->first_same_value)
  5965.         {
  5966.           /* The REG_EQUAL is indicating that two formerly distinct
  5967.          classes are now equivalent.  So merge them.  */
  5968.           merge_equiv_classes (elt, src_eqv_elt);
  5969.           src_eqv_hash_code = HASH (src_eqv, elt->mode);
  5970.           src_eqv_elt = lookup (src_eqv, src_eqv_hash_code, elt->mode);
  5971.         }
  5972.  
  5973.           src_eqv_here = 0;
  5974.         }
  5975.  
  5976.       else if (src_eqv_elt)
  5977.         elt = src_eqv_elt;
  5978.  
  5979.       /* Try to find a constant somewhere and record it in `src_const'.
  5980.      Record its table element, if any, in `src_const_elt'.  Look in
  5981.      any known equivalences first.  (If the constant is not in the
  5982.      table, also set `sets[i].src_const_hash_code').  */
  5983.       if (elt)
  5984.         for (p = elt->first_same_value; p; p = p->next_same_value)
  5985.       if (p->is_const)
  5986.         {
  5987.           src_const = p->exp;
  5988.           src_const_elt = elt;
  5989.           break;
  5990.         }
  5991.  
  5992.       if (src_const == 0
  5993.       && (CONSTANT_P (src_folded)
  5994.           /* Consider (minus (label_ref L1) (label_ref L2)) as 
  5995.          "constant" here so we will record it. This allows us
  5996.          to fold switch statements when an ADDR_DIFF_VEC is used.  */
  5997.           || (GET_CODE (src_folded) == MINUS
  5998.           && GET_CODE (XEXP (src_folded, 0)) == LABEL_REF
  5999.           && GET_CODE (XEXP (src_folded, 1)) == LABEL_REF)))
  6000.     src_const = src_folded, src_const_elt = elt;
  6001.       else if (src_const == 0 && src_eqv_here && CONSTANT_P (src_eqv_here))
  6002.     src_const = src_eqv_here, src_const_elt = src_eqv_elt;
  6003.  
  6004.       /* If we don't know if the constant is in the table, get its
  6005.      hash code and look it up.  */
  6006.       if (src_const && src_const_elt == 0)
  6007.     {
  6008.       sets[i].src_const_hash_code = HASH (src_const, mode);
  6009.       src_const_elt = lookup (src_const, sets[i].src_const_hash_code,
  6010.                   mode);
  6011.     }
  6012.  
  6013.       sets[i].src_const = src_const;
  6014.       sets[i].src_const_elt = src_const_elt;
  6015.  
  6016.       /* If the constant and our source are both in the table, mark them as
  6017.      equivalent.  Otherwise, if a constant is in the table but the source
  6018.      isn't, set ELT to it.  */
  6019.       if (src_const_elt && elt
  6020.       && src_const_elt->first_same_value != elt->first_same_value)
  6021.     merge_equiv_classes (elt, src_const_elt);
  6022.       else if (src_const_elt && elt == 0)
  6023.     elt = src_const_elt;
  6024.  
  6025.       /* See if there is a register linearly related to a constant
  6026.          equivalent of SRC.  */
  6027.       if (src_const
  6028.       && (GET_CODE (src_const) == CONST
  6029.           || (src_const_elt && src_const_elt->related_value != 0)))
  6030.         {
  6031.           src_related = use_related_value (src_const, src_const_elt);
  6032.           if (src_related)
  6033.             {
  6034.           struct table_elt *src_related_elt
  6035.             = lookup (src_related, HASH (src_related, mode), mode);
  6036.           if (src_related_elt && elt)
  6037.             {
  6038.           if (elt->first_same_value
  6039.               != src_related_elt->first_same_value)
  6040.             /* This can occur when we previously saw a CONST 
  6041.                involving a SYMBOL_REF and then see the SYMBOL_REF
  6042.                twice.  Merge the involved classes.  */
  6043.             merge_equiv_classes (elt, src_related_elt);
  6044.  
  6045.               src_related = 0;
  6046.           src_related_elt = 0;
  6047.             }
  6048.               else if (src_related_elt && elt == 0)
  6049.             elt = src_related_elt;
  6050.         }
  6051.         }
  6052.  
  6053.       /* See if we have a CONST_INT that is already in a register in a
  6054.      wider mode.  */
  6055.  
  6056.       if (src_const && src_related == 0 && GET_CODE (src_const) == CONST_INT
  6057.       && GET_MODE_CLASS (mode) == MODE_INT
  6058.       && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
  6059.     {
  6060.       enum machine_mode wider_mode;
  6061.  
  6062.       for (wider_mode = GET_MODE_WIDER_MODE (mode);
  6063.            GET_MODE_BITSIZE (wider_mode) <= BITS_PER_WORD
  6064.            && src_related == 0;
  6065.            wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  6066.         {
  6067.           struct table_elt *const_elt
  6068.         = lookup (src_const, HASH (src_const, wider_mode), wider_mode);
  6069.  
  6070.           if (const_elt == 0)
  6071.         continue;
  6072.  
  6073.           for (const_elt = const_elt->first_same_value;
  6074.            const_elt; const_elt = const_elt->next_same_value)
  6075.         if (GET_CODE (const_elt->exp) == REG)
  6076.           {
  6077.             src_related = gen_lowpart_if_possible (mode,
  6078.                                const_elt->exp);
  6079.             break;
  6080.           }
  6081.         }
  6082.     }
  6083.  
  6084.       /* Another possibility is that we have an AND with a constant in
  6085.      a mode narrower than a word.  If so, it might have been generated
  6086.      as part of an "if" which would narrow the AND.  If we already
  6087.      have done the AND in a wider mode, we can use a SUBREG of that
  6088.      value.  */
  6089.  
  6090.       if (flag_expensive_optimizations && ! src_related
  6091.       && GET_CODE (src) == AND && GET_CODE (XEXP (src, 1)) == CONST_INT
  6092.       && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
  6093.     {
  6094.       enum machine_mode tmode;
  6095.       rtx new_and = gen_rtx (AND, VOIDmode, NULL_RTX, XEXP (src, 1));
  6096.  
  6097.       for (tmode = GET_MODE_WIDER_MODE (mode);
  6098.            GET_MODE_SIZE (tmode) <= UNITS_PER_WORD;
  6099.            tmode = GET_MODE_WIDER_MODE (tmode))
  6100.         {
  6101.           rtx inner = gen_lowpart_if_possible (tmode, XEXP (src, 0));
  6102.           struct table_elt *larger_elt;
  6103.  
  6104.           if (inner)
  6105.         {
  6106.           PUT_MODE (new_and, tmode);
  6107.           XEXP (new_and, 0) = inner;
  6108.           larger_elt = lookup (new_and, HASH (new_and, tmode), tmode);
  6109.           if (larger_elt == 0)
  6110.             continue;
  6111.  
  6112.           for (larger_elt = larger_elt->first_same_value;
  6113.                larger_elt; larger_elt = larger_elt->next_same_value)
  6114.             if (GET_CODE (larger_elt->exp) == REG)
  6115.               {
  6116.             src_related
  6117.               = gen_lowpart_if_possible (mode, larger_elt->exp);
  6118.             break;
  6119.               }
  6120.  
  6121.           if (src_related)
  6122.             break;
  6123.         }
  6124.         }
  6125.     }
  6126.           
  6127.       if (src == src_folded)
  6128.         src_folded = 0;
  6129.  
  6130.       /* At this point, ELT, if non-zero, points to a class of expressions
  6131.          equivalent to the source of this SET and SRC, SRC_EQV, SRC_FOLDED,
  6132.      and SRC_RELATED, if non-zero, each contain additional equivalent
  6133.      expressions.  Prune these latter expressions by deleting expressions
  6134.      already in the equivalence class.
  6135.  
  6136.      Check for an equivalent identical to the destination.  If found,
  6137.      this is the preferred equivalent since it will likely lead to
  6138.      elimination of the insn.  Indicate this by placing it in
  6139.      `src_related'.  */
  6140.  
  6141.       if (elt) elt = elt->first_same_value;
  6142.       for (p = elt; p; p = p->next_same_value)
  6143.         {
  6144.       enum rtx_code code = GET_CODE (p->exp);
  6145.  
  6146.       /* If the expression is not valid, ignore it.  Then we do not
  6147.          have to check for validity below.  In most cases, we can use
  6148.          `rtx_equal_p', since canonicalization has already been done.  */
  6149.       if (code != REG && ! exp_equiv_p (p->exp, p->exp, 1, 0))
  6150.         continue;
  6151.  
  6152.           if (src && GET_CODE (src) == code && rtx_equal_p (src, p->exp))
  6153.         src = 0;
  6154.           else if (src_folded && GET_CODE (src_folded) == code
  6155.            && rtx_equal_p (src_folded, p->exp))
  6156.         src_folded = 0;
  6157.           else if (src_eqv_here && GET_CODE (src_eqv_here) == code
  6158.            && rtx_equal_p (src_eqv_here, p->exp))
  6159.         src_eqv_here = 0;
  6160.           else if (src_related && GET_CODE (src_related) == code
  6161.            && rtx_equal_p (src_related, p->exp))
  6162.         src_related = 0;
  6163.  
  6164.       /* This is the same as the destination of the insns, we want
  6165.          to prefer it.  Copy it to src_related.  The code below will
  6166.          then give it a negative cost.  */
  6167.       if (GET_CODE (dest) == code && rtx_equal_p (p->exp, dest))
  6168.         src_related = dest;
  6169.  
  6170.         }
  6171.  
  6172.       /* Find the cheapest valid equivalent, trying all the available
  6173.          possibilities.  Prefer items not in the hash table to ones
  6174.          that are when they are equal cost.  Note that we can never
  6175.          worsen an insn as the current contents will also succeed.
  6176.      If we find an equivalent identical to the destination, use it as best,
  6177.      since this insn will probably be eliminated in that case. */
  6178.       if (src)
  6179.     {
  6180.       if (rtx_equal_p (src, dest))
  6181.         src_cost = -1;
  6182.       else
  6183.         src_cost = COST (src);
  6184.     }
  6185.  
  6186.       if (src_eqv_here)
  6187.     {
  6188.       if (rtx_equal_p (src_eqv_here, dest))
  6189.         src_eqv_cost = -1;
  6190.       else
  6191.         src_eqv_cost = COST (src_eqv_here);
  6192.     }
  6193.  
  6194.       if (src_folded)
  6195.     {
  6196.       if (rtx_equal_p (src_folded, dest))
  6197.         src_folded_cost = -1;
  6198.       else
  6199.         src_folded_cost = COST (src_folded);
  6200.     }
  6201.  
  6202.       if (src_related)
  6203.     {
  6204.       if (rtx_equal_p (src_related, dest))
  6205.         src_related_cost = -1;
  6206.       else
  6207.         src_related_cost = COST (src_related);
  6208.     }
  6209.  
  6210.       /* If this was an indirect jump insn, a known label will really be
  6211.      cheaper even though it looks more expensive.  */
  6212.       if (dest == pc_rtx && src_const && GET_CODE (src_const) == LABEL_REF)
  6213.     src_folded = src_const, src_folded_cost = -1;
  6214.       
  6215.       /* Terminate loop when replacement made.  This must terminate since
  6216.          the current contents will be tested and will always be valid.  */
  6217.       while (1)
  6218.         {
  6219.           rtx trial;
  6220.  
  6221.           /* Skip invalid entries.  */
  6222.           while (elt && GET_CODE (elt->exp) != REG
  6223.              && ! exp_equiv_p (elt->exp, elt->exp, 1, 0))
  6224.         elt = elt->next_same_value;         
  6225.           
  6226.           if (elt) src_elt_cost = elt->cost;
  6227.  
  6228.           /* Find cheapest and skip it for the next time.   For items
  6229.          of equal cost, use this order:
  6230.          src_folded, src, src_eqv, src_related and hash table entry.  */
  6231.           if (src_folded_cost <= src_cost
  6232.           && src_folded_cost <= src_eqv_cost
  6233.           && src_folded_cost <= src_related_cost
  6234.           && src_folded_cost <= src_elt_cost)
  6235.         {
  6236.           trial = src_folded, src_folded_cost = 10000;
  6237.           if (src_folded_force_flag)
  6238.         trial = force_const_mem (mode, trial);
  6239.         }
  6240.           else if (src_cost <= src_eqv_cost
  6241.                && src_cost <= src_related_cost
  6242.                && src_cost <= src_elt_cost)
  6243.         trial = src, src_cost = 10000;
  6244.           else if (src_eqv_cost <= src_related_cost
  6245.                && src_eqv_cost <= src_elt_cost)
  6246.         trial = src_eqv_here, src_eqv_cost = 10000;
  6247.           else if (src_related_cost <= src_elt_cost)
  6248.         trial = src_related, src_related_cost = 10000;
  6249.           else
  6250.         {
  6251.           trial = copy_rtx (elt->exp);
  6252.           elt = elt->next_same_value;
  6253.           src_elt_cost = 10000;
  6254.         }
  6255.  
  6256.       /* We don't normally have an insn matching (set (pc) (pc)), so
  6257.          check for this separately here.  We will delete such an
  6258.          insn below.
  6259.  
  6260.          Tablejump insns contain a USE of the table, so simply replacing
  6261.          the operand with the constant won't match.  This is simply an
  6262.          unconditional branch, however, and is therefore valid.  Just
  6263.          insert the substitution here and we will delete and re-emit
  6264.          the insn later.  */
  6265.  
  6266.       if (n_sets == 1 && dest == pc_rtx
  6267.           && (trial == pc_rtx
  6268.           || (GET_CODE (trial) == LABEL_REF
  6269.               && ! condjump_p (insn))))
  6270.         {
  6271.           /* If TRIAL is a label in front of a jump table, we are
  6272.          really falling through the switch (this is how casesi
  6273.          insns work), so we must branch around the table.  */
  6274.           if (GET_CODE (trial) == CODE_LABEL
  6275.           && NEXT_INSN (trial) != 0
  6276.           && GET_CODE (NEXT_INSN (trial)) == JUMP_INSN
  6277.           && (GET_CODE (PATTERN (NEXT_INSN (trial))) == ADDR_DIFF_VEC
  6278.               || GET_CODE (PATTERN (NEXT_INSN (trial))) == ADDR_VEC))
  6279.  
  6280.         trial = gen_rtx (LABEL_REF, TPmode, get_label_after (trial));
  6281.  
  6282.           SET_SRC (sets[i].rtl) = trial;
  6283.           break;
  6284.         }
  6285.        
  6286.       /* Look for a substitution that makes a valid insn.  */
  6287.           else if (validate_change (insn, &SET_SRC (sets[i].rtl), trial, 0))
  6288.         {
  6289.           /* The result of apply_change_group can be ignored; see
  6290.          canon_reg.  */
  6291.  
  6292.           validate_change (insn, &SET_SRC (sets[i].rtl),
  6293.                    canon_reg (SET_SRC (sets[i].rtl), insn),
  6294.                    1);
  6295.           apply_change_group ();
  6296.           break;
  6297.         }
  6298.  
  6299.       /* If we previously found constant pool entries for 
  6300.          constants and this is a constant, try making a
  6301.          pool entry.  Put it in src_folded unless we already have done
  6302.          this since that is where it likely came from.  */
  6303.  
  6304.       else if (constant_pool_entries_cost
  6305.            && CONSTANT_P (trial)
  6306.            && (src_folded == 0 || GET_CODE (src_folded) != MEM)
  6307.            && GET_MODE_CLASS (mode) != MODE_CC)
  6308.         {
  6309.           src_folded_force_flag = 1;
  6310.           src_folded = trial;
  6311.           src_folded_cost = constant_pool_entries_cost;
  6312.         }
  6313.         }
  6314.  
  6315.       src = SET_SRC (sets[i].rtl);
  6316.  
  6317.       /* In general, it is good to have a SET with SET_SRC == SET_DEST.
  6318.      However, there is an important exception:  If both are registers
  6319.      that are not the head of their equivalence class, replace SET_SRC
  6320.      with the head of the class.  If we do not do this, we will have
  6321.      both registers live over a portion of the basic block.  This way,
  6322.      their lifetimes will likely abut instead of overlapping.  */
  6323.       if (GET_CODE (dest) == REG
  6324.       && REGNO_QTY_VALID_P (REGNO (dest))
  6325.       && qty_mode[reg_qty[REGNO (dest)]] == GET_MODE (dest)
  6326.       && qty_first_reg[reg_qty[REGNO (dest)]] != REGNO (dest)
  6327.       && GET_CODE (src) == REG && REGNO (src) == REGNO (dest)
  6328.       /* Don't do this if the original insn had a hard reg as
  6329.          SET_SRC.  */
  6330.       && (GET_CODE (sets[i].src) != REG
  6331.           || REGNO (sets[i].src) >= FIRST_PSEUDO_REGISTER))
  6332.     /* We can't call canon_reg here because it won't do anything if
  6333.        SRC is a hard register.  */
  6334.     {
  6335.       int first = qty_first_reg[reg_qty[REGNO (src)]];
  6336.  
  6337.       src = SET_SRC (sets[i].rtl)
  6338.         = first >= FIRST_PSEUDO_REGISTER ? regno_reg_rtx[first]
  6339.           : gen_rtx (REG, GET_MODE (src), first);
  6340.  
  6341.       /* If we had a constant that is cheaper than what we are now
  6342.          setting SRC to, use that constant.  We ignored it when we
  6343.          thought we could make this into a no-op.  */
  6344.       if (src_const && COST (src_const) < COST (src)
  6345.           && validate_change (insn, &SET_SRC (sets[i].rtl), src_const, 0))
  6346.         src = src_const;
  6347.     }
  6348.  
  6349.       /* If we made a change, recompute SRC values.  */
  6350.       if (src != sets[i].src)
  6351.         {
  6352.           do_not_record = 0;
  6353.           hash_arg_in_memory = 0;
  6354.           hash_arg_in_struct = 0;
  6355.       sets[i].src = src;
  6356.           sets[i].src_hash_code = HASH (src, mode);
  6357.           sets[i].src_volatile = do_not_record;
  6358.           sets[i].src_in_memory = hash_arg_in_memory;
  6359.           sets[i].src_in_struct = hash_arg_in_struct;
  6360.           sets[i].src_elt = lookup (src, sets[i].src_hash_code, mode);
  6361.         }
  6362.  
  6363.       /* If this is a single SET, we are setting a register, and we have an
  6364.      equivalent constant, we want to add a REG_NOTE.   We don't want
  6365.      to write a REG_EQUAL note for a constant pseudo since verifying that
  6366.      that pseudo hasn't been eliminated is a pain.  Such a note also
  6367.      won't help anything.  */
  6368.       if (n_sets == 1 && src_const && GET_CODE (dest) == REG
  6369.       && GET_CODE (src_const) != REG)
  6370.     {
  6371.       rtx tem = find_reg_note (insn, REG_EQUAL, NULL_RTX);
  6372.       
  6373.       /* Record the actual constant value in a REG_EQUAL note, making
  6374.          a new one if one does not already exist.  */
  6375.       if (tem)
  6376.         XEXP (tem, 0) = src_const;
  6377.       else
  6378.         REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUAL,
  6379.                         src_const, REG_NOTES (insn));
  6380.  
  6381.           /* If storing a constant value in a register that
  6382.          previously held the constant value 0,
  6383.          record this fact with a REG_WAS_0 note on this insn.
  6384.  
  6385.          Note that the *register* is required to have previously held 0,
  6386.          not just any register in the quantity and we must point to the
  6387.          insn that set that register to zero.
  6388.  
  6389.          Rather than track each register individually, we just see if
  6390.          the last set for this quantity was for this register.  */
  6391.  
  6392.       if (REGNO_QTY_VALID_P (REGNO (dest))
  6393.           && qty_const[reg_qty[REGNO (dest)]] == const0_rtx)
  6394.         {
  6395.           /* See if we previously had a REG_WAS_0 note.  */
  6396.           rtx note = find_reg_note (insn, REG_WAS_0, NULL_RTX);
  6397.           rtx const_insn = qty_const_insn[reg_qty[REGNO (dest)]];
  6398.  
  6399.           if ((tem = single_set (const_insn)) != 0
  6400.           && rtx_equal_p (SET_DEST (tem), dest))
  6401.         {
  6402.           if (note)
  6403.             XEXP (note, 0) = const_insn;
  6404.           else
  6405.             REG_NOTES (insn) = gen_rtx (INSN_LIST, REG_WAS_0,
  6406.                         const_insn, REG_NOTES (insn));
  6407.         }
  6408.         }
  6409.     }
  6410.  
  6411.       /* Now deal with the destination.  */
  6412.       do_not_record = 0;
  6413.       sets[i].inner_dest_loc = &SET_DEST (sets[0].rtl);
  6414.  
  6415.       /* Look within any SIGN_EXTRACT or ZERO_EXTRACT
  6416.      to the MEM or REG within it.  */
  6417.       while (GET_CODE (dest) == SIGN_EXTRACT
  6418.          || GET_CODE (dest) == ZERO_EXTRACT
  6419.          || GET_CODE (dest) == SUBREG
  6420.          || GET_CODE (dest) == STRICT_LOW_PART)
  6421.     {
  6422.       sets[i].inner_dest_loc = &XEXP (dest, 0);
  6423.       dest = XEXP (dest, 0);
  6424.     }
  6425.  
  6426.       sets[i].inner_dest = dest;
  6427.  
  6428.       if (GET_CODE (dest) == MEM)
  6429.     {
  6430.       dest = fold_rtx (dest, insn);
  6431.  
  6432.       /* Decide whether we invalidate everything in memory,
  6433.          or just things at non-fixed places.
  6434.          Writing a large aggregate must invalidate everything
  6435.          because we don't know how long it is.  */
  6436.       note_mem_written (dest, &writes_memory);
  6437.     }
  6438.  
  6439.       /* Compute the hash code of the destination now,
  6440.      before the effects of this instruction are recorded,
  6441.      since the register values used in the address computation
  6442.      are those before this instruction.  */
  6443.       sets[i].dest_hash_code = HASH (dest, mode);
  6444.  
  6445.       /* Don't enter a bit-field in the hash table
  6446.      because the value in it after the store
  6447.      may not equal what was stored, due to truncation.  */
  6448.  
  6449.       if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT
  6450.       || GET_CODE (SET_DEST (sets[i].rtl)) == SIGN_EXTRACT)
  6451.     {
  6452.       rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
  6453.  
  6454.       if (src_const != 0 && GET_CODE (src_const) == CONST_INT
  6455.           && GET_CODE (width) == CONST_INT
  6456.           && INTVAL (width) < HOST_BITS_PER_WIDE_INT
  6457.           && ! (INTVAL (src_const)
  6458.             & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
  6459.         /* Exception: if the value is constant,
  6460.            and it won't be truncated, record it.  */
  6461.         ;
  6462.       else
  6463.         {
  6464.           /* This is chosen so that the destination will be invalidated
  6465.          but no new value will be recorded.
  6466.          We must invalidate because sometimes constant
  6467.          values can be recorded for bitfields.  */
  6468.           sets[i].src_elt = 0;
  6469.           sets[i].src_volatile = 1;
  6470.           src_eqv = 0;
  6471.           src_eqv_elt = 0;
  6472.         }
  6473.     }
  6474.  
  6475.       /* If only one set in a JUMP_INSN and it is now a no-op, we can delete
  6476.      the insn.  */
  6477.       else if (n_sets == 1 && dest == pc_rtx && src == pc_rtx)
  6478.     {
  6479.       PUT_CODE (insn, NOTE);
  6480.       NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  6481.       NOTE_SOURCE_FILE (insn) = 0;
  6482.       cse_jumps_altered = 1;
  6483.       /* One less use of the label this insn used to jump to.  */
  6484.       --LABEL_NUSES (JUMP_LABEL (insn));
  6485.       /* No more processing for this set.  */
  6486.       sets[i].rtl = 0;
  6487.     }
  6488.  
  6489.       /* If this SET is now setting PC to a label, we know it used to
  6490.      be a conditional or computed branch.  So we see if we can follow
  6491.      it.  If it was a computed branch, delete it and re-emit.  */
  6492.       else if (dest == pc_rtx && GET_CODE (src) == LABEL_REF)
  6493.     {
  6494.       rtx p;
  6495.  
  6496.       /* If this is not in the format for a simple branch and
  6497.          we are the only SET in it, re-emit it.  */
  6498.       if (! simplejump_p (insn) && n_sets == 1)
  6499.         {
  6500.           rtx new = emit_jump_insn_before (gen_jump (XEXP (src, 0)), insn);
  6501.           JUMP_LABEL (new) = XEXP (src, 0);
  6502.           LABEL_NUSES (XEXP (src, 0))++;
  6503.           delete_insn (insn);
  6504.           insn = new;
  6505.         }
  6506.  
  6507.       /* Now that we've converted this jump to an unconditional jump,
  6508.          there is dead code after it.  Delete the dead code until we
  6509.          reach a BARRIER, the end of the function, or a label.  Do
  6510.          not delete NOTEs except for NOTE_INSN_DELETED since later
  6511.          phases assume these notes are retained.  */
  6512.  
  6513.       p = insn;
  6514.  
  6515.       while (NEXT_INSN (p) != 0
  6516.          && GET_CODE (NEXT_INSN (p)) != BARRIER
  6517.          && GET_CODE (NEXT_INSN (p)) != CODE_LABEL)
  6518.         {
  6519.           if (GET_CODE (NEXT_INSN (p)) != NOTE
  6520.           || NOTE_LINE_NUMBER (NEXT_INSN (p)) == NOTE_INSN_DELETED)
  6521.         delete_insn (NEXT_INSN (p));
  6522.           else
  6523.         p = NEXT_INSN (p);
  6524.         }
  6525.  
  6526.       /* If we don't have a BARRIER immediately after INSN, put one there.
  6527.          Much code assumes that there are no NOTEs between a JUMP_INSN and
  6528.          BARRIER.  */
  6529.  
  6530.       if (NEXT_INSN (insn) == 0
  6531.           || GET_CODE (NEXT_INSN (insn)) != BARRIER)
  6532.         emit_barrier_after (insn);
  6533.  
  6534.       /* We might have two BARRIERs separated by notes.  Delete the second
  6535.          one if so.  */
  6536.  
  6537.       if (p != insn && NEXT_INSN (p) != 0
  6538.           && GET_CODE (NEXT_INSN (p)) == BARRIER)
  6539.         delete_insn (NEXT_INSN (p));
  6540.  
  6541.       cse_jumps_altered = 1;
  6542.       sets[i].rtl = 0;
  6543.     }
  6544.  
  6545.       /* If destination is volatile, invalidate it and then do no further
  6546.      processing for this assignment.  */
  6547.  
  6548.       else if (do_not_record)
  6549.     {
  6550.       if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
  6551.           || GET_CODE (dest) == MEM)
  6552.         invalidate (dest);
  6553.       sets[i].rtl = 0;
  6554.     }
  6555.  
  6556.       if (sets[i].rtl != 0 && dest != SET_DEST (sets[i].rtl))
  6557.     sets[i].dest_hash_code = HASH (SET_DEST (sets[i].rtl), mode);
  6558.  
  6559. #ifdef HAVE_cc0
  6560.       /* If setting CC0, record what it was set to, or a constant, if it
  6561.      is equivalent to a constant.  If it is being set to a floating-point
  6562.      value, make a COMPARE with the appropriate constant of 0.  If we
  6563.      don't do this, later code can interpret this as a test against
  6564.      const0_rtx, which can cause problems if we try to put it into an
  6565.      insn as a floating-point operand.  */
  6566.       if (dest == cc0_rtx)
  6567.     {
  6568.       this_insn_cc0 = src_const && mode != VOIDmode ? src_const : src;
  6569.       this_insn_cc0_mode = mode;
  6570.       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  6571.         this_insn_cc0 = gen_rtx (COMPARE, VOIDmode, this_insn_cc0,
  6572.                      CONST0_RTX (mode));
  6573.     }
  6574. #endif
  6575.     }
  6576.  
  6577.   /* Now enter all non-volatile source expressions in the hash table
  6578.      if they are not already present.
  6579.      Record their equivalence classes in src_elt.
  6580.      This way we can insert the corresponding destinations into
  6581.      the same classes even if the actual sources are no longer in them
  6582.      (having been invalidated).  */
  6583.  
  6584.   if (src_eqv && src_eqv_elt == 0 && sets[0].rtl != 0 && ! src_eqv_volatile
  6585.       && ! rtx_equal_p (src_eqv, SET_DEST (sets[0].rtl)))
  6586.     {
  6587.       register struct table_elt *elt;
  6588.       register struct table_elt *classp = sets[0].src_elt;
  6589.       rtx dest = SET_DEST (sets[0].rtl);
  6590.       enum machine_mode eqvmode = GET_MODE (dest);
  6591.  
  6592.       if (GET_CODE (dest) == STRICT_LOW_PART)
  6593.     {
  6594.       eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
  6595.       classp = 0;
  6596.     }
  6597.       if (insert_regs (src_eqv, classp, 0))
  6598.     src_eqv_hash_code = HASH (src_eqv, eqvmode);
  6599.       elt = insert (src_eqv, classp, src_eqv_hash_code, eqvmode);
  6600.       elt->in_memory = src_eqv_in_memory;
  6601.       elt->in_struct = src_eqv_in_struct;
  6602.       src_eqv_elt = elt;
  6603.     }
  6604.  
  6605.   for (i = 0; i < n_sets; i++)
  6606.     if (sets[i].rtl && ! sets[i].src_volatile
  6607.     && ! rtx_equal_p (SET_SRC (sets[i].rtl), SET_DEST (sets[i].rtl)))
  6608.       {
  6609.     if (GET_CODE (SET_DEST (sets[i].rtl)) == STRICT_LOW_PART)
  6610.       {
  6611.         /* REG_EQUAL in setting a STRICT_LOW_PART
  6612.            gives an equivalent for the entire destination register,
  6613.            not just for the subreg being stored in now.
  6614.            This is a more interesting equivalence, so we arrange later
  6615.            to treat the entire reg as the destination.  */
  6616.         sets[i].src_elt = src_eqv_elt;
  6617.         sets[i].src_hash_code = src_eqv_hash_code;
  6618.       }
  6619.     else
  6620.       {
  6621.         /* Insert source and constant equivalent into hash table, if not
  6622.            already present.  */
  6623.         register struct table_elt *classp = src_eqv_elt;
  6624.         register rtx src = sets[i].src;
  6625.         register rtx dest = SET_DEST (sets[i].rtl);
  6626.         enum machine_mode mode
  6627.           = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
  6628.  
  6629.         if (sets[i].src_elt == 0)
  6630.           {
  6631.         register struct table_elt *elt;
  6632.  
  6633.         /* Note that these insert_regs calls cannot remove
  6634.            any of the src_elt's, because they would have failed to
  6635.            match if not still valid.  */
  6636.         if (insert_regs (src, classp, 0))
  6637.           sets[i].src_hash_code = HASH (src, mode);
  6638.         elt = insert (src, classp, sets[i].src_hash_code, mode);
  6639.         elt->in_memory = sets[i].src_in_memory;
  6640.         elt->in_struct = sets[i].src_in_struct;
  6641.         sets[i].src_elt = classp = elt;
  6642.           }
  6643.  
  6644.         if (sets[i].src_const && sets[i].src_const_elt == 0
  6645.         && src != sets[i].src_const
  6646.         && ! rtx_equal_p (sets[i].src_const, src))
  6647.           sets[i].src_elt = insert (sets[i].src_const, classp,
  6648.                     sets[i].src_const_hash_code, mode);
  6649.       }
  6650.       }
  6651.     else if (sets[i].src_elt == 0)
  6652.       /* If we did not insert the source into the hash table (e.g., it was
  6653.      volatile), note the equivalence class for the REG_EQUAL value, if any,
  6654.      so that the destination goes into that class.  */
  6655.       sets[i].src_elt = src_eqv_elt;
  6656.  
  6657.   invalidate_from_clobbers (&writes_memory, x);
  6658.  
  6659.   /* Some registers are invalidated by subroutine calls.  Memory is 
  6660.      invalidated by non-constant calls.  */
  6661.  
  6662.   if (GET_CODE (insn) == CALL_INSN)
  6663.     {
  6664.       static struct write_data everything = {0, 1, 1, 1};
  6665.  
  6666.       if (! CONST_CALL_P (insn))
  6667.     invalidate_memory (&everything);
  6668.       invalidate_for_call ();
  6669.     }
  6670.  
  6671.   /* Now invalidate everything set by this instruction.
  6672.      If a SUBREG or other funny destination is being set,
  6673.      sets[i].rtl is still nonzero, so here we invalidate the reg
  6674.      a part of which is being set.  */
  6675.  
  6676.   for (i = 0; i < n_sets; i++)
  6677.     if (sets[i].rtl)
  6678.       {
  6679.     register rtx dest = sets[i].inner_dest;
  6680.  
  6681.     /* Needed for registers to remove the register from its
  6682.        previous quantity's chain.
  6683.        Needed for memory if this is a nonvarying address, unless
  6684.        we have just done an invalidate_memory that covers even those.  */
  6685.     if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
  6686.         || (! writes_memory.all && ! cse_rtx_addr_varies_p (dest)))
  6687.       invalidate (dest);
  6688.       }
  6689.  
  6690.   /* Make sure registers mentioned in destinations
  6691.      are safe for use in an expression to be inserted.
  6692.      This removes from the hash table
  6693.      any invalid entry that refers to one of these registers.
  6694.  
  6695.      We don't care about the return value from mention_regs because
  6696.      we are going to hash the SET_DEST values unconditionally.  */
  6697.  
  6698.   for (i = 0; i < n_sets; i++)
  6699.     if (sets[i].rtl && GET_CODE (SET_DEST (sets[i].rtl)) != REG)
  6700.       mention_regs (SET_DEST (sets[i].rtl));
  6701.  
  6702.   /* We may have just removed some of the src_elt's from the hash table.
  6703.      So replace each one with the current head of the same class.  */
  6704.  
  6705.   for (i = 0; i < n_sets; i++)
  6706.     if (sets[i].rtl)
  6707.       {
  6708.     if (sets[i].src_elt && sets[i].src_elt->first_same_value == 0)
  6709.       /* If elt was removed, find current head of same class,
  6710.          or 0 if nothing remains of that class.  */
  6711.       {
  6712.         register struct table_elt *elt = sets[i].src_elt;
  6713.  
  6714.         while (elt && elt->prev_same_value)
  6715.           elt = elt->prev_same_value;
  6716.  
  6717.         while (elt && elt->first_same_value == 0)
  6718.           elt = elt->next_same_value;
  6719.         sets[i].src_elt = elt ? elt->first_same_value : 0;
  6720.       }
  6721.       }
  6722.  
  6723.   /* Now insert the destinations into their equivalence classes.  */
  6724.  
  6725.   for (i = 0; i < n_sets; i++)
  6726.     if (sets[i].rtl)
  6727.       {
  6728.     register rtx dest = SET_DEST (sets[i].rtl);
  6729.     register struct table_elt *elt;
  6730.  
  6731.     /* Don't record value if we are not supposed to risk allocating
  6732.        floating-point values in registers that might be wider than
  6733.        memory.  */
  6734.     if ((flag_float_store
  6735.          && GET_CODE (dest) == MEM
  6736.          && GET_MODE_CLASS (GET_MODE (dest)) == MODE_FLOAT)
  6737.         /* Don't record values of destinations set inside a libcall block
  6738.            since we might delete the libcall.  Things should have been set
  6739.            up so we won't want to reuse such a value, but we play it safe
  6740.            here.  */
  6741.         || in_libcall_block
  6742.         /* If we didn't put a REG_EQUAL value or a source into the hash
  6743.            table, there is no point is recording DEST.  */
  6744.          || sets[i].src_elt == 0)
  6745.       continue;
  6746.  
  6747.     /* STRICT_LOW_PART isn't part of the value BEING set,
  6748.        and neither is the SUBREG inside it.
  6749.        Note that in this case SETS[I].SRC_ELT is really SRC_EQV_ELT.  */
  6750.     if (GET_CODE (dest) == STRICT_LOW_PART)
  6751.       dest = SUBREG_REG (XEXP (dest, 0));
  6752.  
  6753.     if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG)
  6754.       /* Registers must also be inserted into chains for quantities.  */
  6755.       if (insert_regs (dest, sets[i].src_elt, 1))
  6756.         /* If `insert_regs' changes something, the hash code must be
  6757.            recalculated.  */
  6758.         sets[i].dest_hash_code = HASH (dest, GET_MODE (dest));
  6759.  
  6760.     elt = insert (dest, sets[i].src_elt,
  6761.               sets[i].dest_hash_code, GET_MODE (dest));
  6762.     elt->in_memory = GET_CODE (sets[i].inner_dest) == MEM;
  6763.     if (elt->in_memory)
  6764.       {
  6765.         /* This implicitly assumes a whole struct
  6766.            need not have MEM_IN_STRUCT_P.
  6767.            But a whole struct is *supposed* to have MEM_IN_STRUCT_P.  */
  6768.         elt->in_struct = (MEM_IN_STRUCT_P (sets[i].inner_dest)
  6769.                   || sets[i].inner_dest != SET_DEST (sets[i].rtl));
  6770.       }
  6771.  
  6772.     /* If we have (set (subreg:m1 (reg:m2 foo) 0) (bar:m1)), M1 is no
  6773.        narrower than M2, and both M1 and M2 are the same number of words,
  6774.        we are also doing (set (reg:m2 foo) (subreg:m2 (bar:m1) 0)) so
  6775.        make that equivalence as well.
  6776.  
  6777.        However, BAR may have equivalences for which gen_lowpart_if_possible
  6778.        will produce a simpler value than gen_lowpart_if_possible applied to
  6779.        BAR (e.g., if BAR was ZERO_EXTENDed from M2), so we will scan all
  6780.        BAR's equivalences.  If we don't get a simplified form, make 
  6781.        the SUBREG.  It will not be used in an equivalence, but will
  6782.        cause two similar assignments to be detected.
  6783.  
  6784.        Note the loop below will find SUBREG_REG (DEST) since we have
  6785.        already entered SRC and DEST of the SET in the table.  */
  6786.  
  6787.     if (GET_CODE (dest) == SUBREG
  6788.         && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))) / UNITS_PER_WORD
  6789.         == GET_MODE_SIZE (GET_MODE (dest)) / UNITS_PER_WORD)
  6790.         && (GET_MODE_SIZE (GET_MODE (dest))
  6791.         >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
  6792.         && sets[i].src_elt != 0)
  6793.       {
  6794.         enum machine_mode new_mode = GET_MODE (SUBREG_REG (dest));
  6795.         struct table_elt *elt, *classp = 0;
  6796.  
  6797.         for (elt = sets[i].src_elt->first_same_value; elt;
  6798.          elt = elt->next_same_value)
  6799.           {
  6800.         rtx new_src = 0;
  6801.         int src_hash;
  6802.         struct table_elt *src_elt;
  6803.  
  6804.         /* Ignore invalid entries.  */
  6805.         if (GET_CODE (elt->exp) != REG
  6806.             && ! exp_equiv_p (elt->exp, elt->exp, 1, 0))
  6807.           continue;
  6808.  
  6809.         new_src = gen_lowpart_if_possible (new_mode, elt->exp);
  6810.         if (new_src == 0)
  6811.           new_src = gen_rtx (SUBREG, new_mode, elt->exp, 0);
  6812.  
  6813.         src_hash = HASH (new_src, new_mode);
  6814.         src_elt = lookup (new_src, src_hash, new_mode);
  6815.  
  6816.         /* Put the new source in the hash table is if isn't
  6817.            already.  */
  6818.         if (src_elt == 0)
  6819.           {
  6820.             if (insert_regs (new_src, classp, 0))
  6821.               src_hash = HASH (new_src, new_mode);
  6822.             src_elt = insert (new_src, classp, src_hash, new_mode);
  6823.             src_elt->in_memory = elt->in_memory;
  6824.             src_elt->in_struct = elt->in_struct;
  6825.           }
  6826.         else if (classp && classp != src_elt->first_same_value)
  6827.           /* Show that two things that we've seen before are 
  6828.              actually the same.  */
  6829.           merge_equiv_classes (src_elt, classp);
  6830.  
  6831.         classp = src_elt->first_same_value;
  6832.           }
  6833.       }
  6834.       }
  6835.  
  6836.   /* Special handling for (set REG0 REG1)
  6837.      where REG0 is the "cheapest", cheaper than REG1.
  6838.      After cse, REG1 will probably not be used in the sequel, 
  6839.      so (if easily done) change this insn to (set REG1 REG0) and
  6840.      replace REG1 with REG0 in the previous insn that computed their value.
  6841.      Then REG1 will become a dead store and won't cloud the situation
  6842.      for later optimizations.
  6843.  
  6844.      Do not make this change if REG1 is a hard register, because it will
  6845.      then be used in the sequel and we may be changing a two-operand insn
  6846.      into a three-operand insn.
  6847.  
  6848.      Also do not do this if we are operating on a copy of INSN.  */
  6849.  
  6850.   if (n_sets == 1 && sets[0].rtl && GET_CODE (SET_DEST (sets[0].rtl)) == REG
  6851.       && NEXT_INSN (PREV_INSN (insn)) == insn
  6852.       && GET_CODE (SET_SRC (sets[0].rtl)) == REG
  6853.       && REGNO (SET_SRC (sets[0].rtl)) >= FIRST_PSEUDO_REGISTER
  6854.       && REGNO_QTY_VALID_P (REGNO (SET_SRC (sets[0].rtl)))
  6855.       && (qty_first_reg[reg_qty[REGNO (SET_SRC (sets[0].rtl))]]
  6856.       == REGNO (SET_DEST (sets[0].rtl))))
  6857.     {
  6858.       rtx prev = PREV_INSN (insn);
  6859.       while (prev && GET_CODE (prev) == NOTE)
  6860.     prev = PREV_INSN (prev);
  6861.  
  6862.       if (prev && GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SET
  6863.       && SET_DEST (PATTERN (prev)) == SET_SRC (sets[0].rtl))
  6864.     {
  6865.       rtx dest = SET_DEST (sets[0].rtl);
  6866.       rtx note = find_reg_note (prev, REG_EQUIV, NULL_RTX);
  6867.  
  6868.       validate_change (prev, & SET_DEST (PATTERN (prev)), dest, 1);
  6869.       validate_change (insn, & SET_DEST (sets[0].rtl),
  6870.                SET_SRC (sets[0].rtl), 1);
  6871.       validate_change (insn, & SET_SRC (sets[0].rtl), dest, 1);
  6872.       apply_change_group ();
  6873.  
  6874.       /* If REG1 was equivalent to a constant, REG0 is not.  */
  6875.       if (note)
  6876.         PUT_REG_NOTE_KIND (note, REG_EQUAL);
  6877.  
  6878.       /* If there was a REG_WAS_0 note on PREV, remove it.  Move
  6879.          any REG_WAS_0 note on INSN to PREV.  */
  6880.       note = find_reg_note (prev, REG_WAS_0, NULL_RTX);
  6881.       if (note)
  6882.         remove_note (prev, note);
  6883.  
  6884.       note = find_reg_note (insn, REG_WAS_0, NULL_RTX);
  6885.       if (note)
  6886.         {
  6887.           remove_note (insn, note);
  6888.           XEXP (note, 1) = REG_NOTES (prev);
  6889.           REG_NOTES (prev) = note;
  6890.         }
  6891.     }
  6892.     }
  6893.  
  6894.   /* If this is a conditional jump insn, record any known equivalences due to
  6895.      the condition being tested.  */
  6896.  
  6897.   last_jump_equiv_class = 0;
  6898.   if (GET_CODE (insn) == JUMP_INSN
  6899.       && n_sets == 1 && GET_CODE (x) == SET
  6900.       && GET_CODE (SET_SRC (x)) == IF_THEN_ELSE)
  6901.     record_jump_equiv (insn, 0);
  6902.  
  6903. #ifdef HAVE_cc0
  6904.   /* If the previous insn set CC0 and this insn no longer references CC0,
  6905.      delete the previous insn.  Here we use the fact that nothing expects CC0
  6906.      to be valid over an insn, which is true until the final pass.  */
  6907.   if (prev_insn && GET_CODE (prev_insn) == INSN
  6908.       && (tem = single_set (prev_insn)) != 0
  6909.       && SET_DEST (tem) == cc0_rtx
  6910.       && ! reg_mentioned_p (cc0_rtx, x))
  6911.     {
  6912.       PUT_CODE (prev_insn, NOTE);
  6913.       NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
  6914.       NOTE_SOURCE_FILE (prev_insn) = 0;
  6915.     }
  6916.  
  6917.   prev_insn_cc0 = this_insn_cc0;
  6918.   prev_insn_cc0_mode = this_insn_cc0_mode;
  6919. #endif
  6920.  
  6921.   prev_insn = insn;
  6922. }
  6923.  
  6924. /* Store 1 in *WRITES_PTR for those categories of memory ref
  6925.    that must be invalidated when the expression WRITTEN is stored in.
  6926.    If WRITTEN is null, say everything must be invalidated.  */
  6927.  
  6928. static void
  6929. note_mem_written (written, writes_ptr)
  6930.      rtx written;
  6931.      struct write_data *writes_ptr;
  6932. {
  6933.   static struct write_data everything = {0, 1, 1, 1};
  6934.  
  6935.   if (written == 0)
  6936.     *writes_ptr = everything;
  6937.   else if (GET_CODE (written) == MEM)
  6938.     {
  6939.       /* Pushing or popping the stack invalidates just the stack pointer. */
  6940.       rtx addr = XEXP (written, 0);
  6941.       if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
  6942.        || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
  6943.       && GET_CODE (XEXP (addr, 0)) == REG
  6944.       && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
  6945.     {
  6946.       writes_ptr->sp = 1;
  6947.       return;
  6948.     }
  6949.       else if (GET_MODE (written) == BLKmode)
  6950.     *writes_ptr = everything;
  6951.       else if (cse_rtx_addr_varies_p (written))
  6952.     {
  6953.       /* A varying address that is a sum indicates an array element,
  6954.          and that's just as good as a structure element
  6955.          in implying that we need not invalidate scalar variables.  */
  6956.       if (!(MEM_IN_STRUCT_P (written)
  6957.         || GET_CODE (XEXP (written, 0)) == PLUS))
  6958.         writes_ptr->all = 1;
  6959.       writes_ptr->nonscalar = 1;
  6960.     }
  6961.       writes_ptr->var = 1;
  6962.     }
  6963. }
  6964.  
  6965. /* Perform invalidation on the basis of everything about an insn
  6966.    except for invalidating the actual places that are SET in it.
  6967.    This includes the places CLOBBERed, and anything that might
  6968.    alias with something that is SET or CLOBBERed.
  6969.  
  6970.    W points to the writes_memory for this insn, a struct write_data
  6971.    saying which kinds of memory references must be invalidated.
  6972.    X is the pattern of the insn.  */
  6973.  
  6974. static void
  6975. invalidate_from_clobbers (w, x)
  6976.      struct write_data *w;
  6977.      rtx x;
  6978. {
  6979.   /* If W->var is not set, W specifies no action.
  6980.      If W->all is set, this step gets all memory refs
  6981.      so they can be ignored in the rest of this function.  */
  6982.   if (w->var)
  6983.     invalidate_memory (w);
  6984.  
  6985.   if (w->sp)
  6986.     {
  6987.       if (reg_tick[STACK_POINTER_REGNUM] >= 0)
  6988.     reg_tick[STACK_POINTER_REGNUM]++;
  6989.  
  6990.       /* This should be *very* rare.  */
  6991.       if (TEST_HARD_REG_BIT (hard_regs_in_table, STACK_POINTER_REGNUM))
  6992.     invalidate (stack_pointer_rtx);
  6993.     }
  6994.  
  6995.   if (GET_CODE (x) == CLOBBER)
  6996.     {
  6997.       rtx ref = XEXP (x, 0);
  6998.       if (ref
  6999.       && (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
  7000.           || (GET_CODE (ref) == MEM && ! w->all)))
  7001.     invalidate (ref);
  7002.     }
  7003.   else if (GET_CODE (x) == PARALLEL)
  7004.     {
  7005.       register int i;
  7006.       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  7007.     {
  7008.       register rtx y = XVECEXP (x, 0, i);
  7009.       if (GET_CODE (y) == CLOBBER)
  7010.         {
  7011.           rtx ref = XEXP (y, 0);
  7012.           if (ref
  7013.           &&(GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
  7014.              || (GET_CODE (ref) == MEM && !w->all)))
  7015.         invalidate (ref);
  7016.         }
  7017.     }
  7018.     }
  7019. }
  7020.  
  7021. /* Process X, part of the REG_NOTES of an insn.  Look at any REG_EQUAL notes
  7022.    and replace any registers in them with either an equivalent constant
  7023.    or the canonical form of the register.  If we are inside an address,
  7024.    only do this if the address remains valid.
  7025.  
  7026.    OBJECT is 0 except when within a MEM in which case it is the MEM.
  7027.  
  7028.    Return the replacement for X.  */
  7029.  
  7030. static rtx
  7031. cse_process_notes (x, object)
  7032.      rtx x;
  7033.      rtx object;
  7034. {
  7035.   enum rtx_code code = GET_CODE (x);
  7036.   char *fmt = GET_RTX_FORMAT (code);
  7037.   int qty;
  7038.   int i;
  7039.  
  7040.   switch (code)
  7041.     {
  7042.     case CONST_INT:
  7043.     case CONST:
  7044.     case SYMBOL_REF:
  7045.     case LABEL_REF:
  7046.     case CONST_DOUBLE:
  7047.     case PC:
  7048.     case CC0:
  7049.     case LO_SUM:
  7050.       return x;
  7051.  
  7052.     case MEM:
  7053.       XEXP (x, 0) = cse_process_notes (XEXP (x, 0), x);
  7054.       return x;
  7055.  
  7056.     case EXPR_LIST:
  7057.     case INSN_LIST:
  7058.       if (REG_NOTE_KIND (x) == REG_EQUAL)
  7059.     XEXP (x, 0) = cse_process_notes (XEXP (x, 0), NULL_RTX);
  7060.       if (XEXP (x, 1))
  7061.     XEXP (x, 1) = cse_process_notes (XEXP (x, 1), NULL_RTX);
  7062.       return x;
  7063.  
  7064.     case SIGN_EXTEND:
  7065.     case ZERO_EXTEND:
  7066.       {
  7067.     rtx new = cse_process_notes (XEXP (x, 0), object);
  7068.     /* We don't substitute VOIDmode constants into these rtx,
  7069.        since they would impede folding.  */
  7070.     if (GET_MODE (new) != VOIDmode)
  7071.       validate_change (object, &XEXP (x, 0), new, 0);
  7072.     return x;
  7073.       }
  7074.  
  7075.     case REG:
  7076.       i = reg_qty[REGNO (x)];
  7077.  
  7078.       /* Return a constant or a constant register.  */
  7079.       if (REGNO_QTY_VALID_P (REGNO (x))
  7080.       && qty_const[i] != 0
  7081.       && (CONSTANT_P (qty_const[i])
  7082.           || GET_CODE (qty_const[i]) == REG))
  7083.     {
  7084.       rtx new = gen_lowpart_if_possible (GET_MODE (x), qty_const[i]);
  7085.       if (new)
  7086.         return new;
  7087.     }
  7088.  
  7089.       /* Otherwise, canonicalize this register.  */
  7090.       return canon_reg (x, NULL_RTX);
  7091.     }
  7092.  
  7093.   for (i = 0; i < GET_RTX_LENGTH (code); i++)
  7094.     if (fmt[i] == 'e')
  7095.       validate_change (object, &XEXP (x, i),
  7096.                cse_process_notes (XEXP (x, i), object), NULL_RTX);
  7097.  
  7098.   return x;
  7099. }
  7100.  
  7101. /* Find common subexpressions between the end test of a loop and the beginning
  7102.    of the loop.  LOOP_START is the CODE_LABEL at the start of a loop.
  7103.  
  7104.    Often we have a loop where an expression in the exit test is used
  7105.    in the body of the loop.  For example "while (*p) *q++ = *p++;".
  7106.    Because of the way we duplicate the loop exit test in front of the loop,
  7107.    however, we don't detect that common subexpression.  This will be caught
  7108.    when global cse is implemented, but this is a quite common case.
  7109.  
  7110.    This function handles the most common cases of these common expressions.
  7111.    It is called after we have processed the basic block ending with the
  7112.    NOTE_INSN_LOOP_END note that ends a loop and the previous JUMP_INSN
  7113.    jumps to a label used only once.  */
  7114.  
  7115. static void
  7116. cse_around_loop (loop_start)
  7117.      rtx loop_start;
  7118. {
  7119.   rtx insn;
  7120.   int i;
  7121.   struct table_elt *p;
  7122.  
  7123.   /* If the jump at the end of the loop doesn't go to the start, we don't
  7124.      do anything.  */
  7125.   for (insn = PREV_INSN (loop_start);
  7126.        insn && (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0);
  7127.        insn = PREV_INSN (insn))
  7128.     ;
  7129.  
  7130.   if (insn == 0
  7131.       || GET_CODE (insn) != NOTE
  7132.       || NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG)
  7133.     return;
  7134.  
  7135.   /* If the last insn of the loop (the end test) was an NE comparison,
  7136.      we will interpret it as an EQ comparison, since we fell through
  7137.      the loop.  Any equivalences resulting from that comparison are
  7138.      therefore not valid and must be invalidated.  */
  7139.   if (last_jump_equiv_class)
  7140.     for (p = last_jump_equiv_class->first_same_value; p;
  7141.      p = p->next_same_value)
  7142.       if (GET_CODE (p->exp) == MEM || GET_CODE (p->exp) == REG
  7143.       || GET_CODE (p->exp) == SUBREG)
  7144.     invalidate (p->exp);
  7145.  
  7146.   /* Process insns starting after LOOP_START until we hit a CALL_INSN or
  7147.      a CODE_LABEL (we could handle a CALL_INSN, but it isn't worth it).
  7148.  
  7149.      The only thing we do with SET_DEST is invalidate entries, so we
  7150.      can safely process each SET in order.  It is slightly less efficient
  7151.      to do so, but we only want to handle the most common cases.  */
  7152.  
  7153.   for (insn = NEXT_INSN (loop_start);
  7154.        GET_CODE (insn) != CALL_INSN && GET_CODE (insn) != CODE_LABEL
  7155.        && ! (GET_CODE (insn) == NOTE
  7156.          && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
  7157.        insn = NEXT_INSN (insn))
  7158.     {
  7159.       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  7160.       && (GET_CODE (PATTERN (insn)) == SET
  7161.           || GET_CODE (PATTERN (insn)) == CLOBBER))
  7162.     cse_set_around_loop (PATTERN (insn), insn, loop_start);
  7163.       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  7164.            && GET_CODE (PATTERN (insn)) == PARALLEL)
  7165.     for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  7166.       if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
  7167.           || GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
  7168.         cse_set_around_loop (XVECEXP (PATTERN (insn), 0, i), insn,
  7169.                  loop_start);
  7170.     }
  7171. }
  7172.  
  7173. /* Variable used for communications between the next two routines.  */
  7174.  
  7175. static struct write_data skipped_writes_memory;
  7176.  
  7177. /* Process one SET of an insn that was skipped.  We ignore CLOBBERs
  7178.    since they are done elsewhere.  This function is called via note_stores.  */
  7179.  
  7180. static void
  7181. invalidate_skipped_set (dest, set)
  7182.      rtx set;
  7183.      rtx dest;
  7184. {
  7185.   if (GET_CODE (set) == CLOBBER
  7186. #ifdef HAVE_cc0
  7187.       || dest == cc0_rtx
  7188. #endif
  7189.       || dest == pc_rtx)
  7190.     return;
  7191.  
  7192.   if (GET_CODE (dest) == MEM)
  7193.     note_mem_written (dest, &skipped_writes_memory);
  7194.  
  7195.   if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
  7196.       || (! skipped_writes_memory.all && ! cse_rtx_addr_varies_p (dest)))
  7197.     invalidate (dest);
  7198. }
  7199.  
  7200. /* Invalidate all insns from START up to the end of the function or the
  7201.    next label.  This called when we wish to CSE around a block that is
  7202.    conditionally executed.  */
  7203.  
  7204. static void
  7205. invalidate_skipped_block (start)
  7206.      rtx start;
  7207. {
  7208.   rtx insn;
  7209.   int i;
  7210.   static struct write_data init = {0, 0, 0, 0};
  7211.   static struct write_data everything = {0, 1, 1, 1};
  7212.  
  7213.   for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
  7214.        insn = NEXT_INSN (insn))
  7215.     {
  7216.       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
  7217.     continue;
  7218.  
  7219.       skipped_writes_memory = init;
  7220.  
  7221.       if (GET_CODE (insn) == CALL_INSN)
  7222.     {
  7223.       invalidate_for_call ();
  7224.       skipped_writes_memory = everything;
  7225.     }
  7226.  
  7227.       note_stores (PATTERN (insn), invalidate_skipped_set);
  7228.       invalidate_from_clobbers (&skipped_writes_memory, PATTERN (insn));
  7229.     }
  7230. }
  7231.  
  7232. /* Used for communication between the following two routines; contains a
  7233.    value to be checked for modification.  */
  7234.  
  7235. static rtx cse_check_loop_start_value;
  7236.  
  7237. /* If modifying X will modify the value in CSE_CHECK_LOOP_START_VALUE,
  7238.    indicate that fact by setting CSE_CHECK_LOOP_START_VALUE to 0.  */
  7239.  
  7240. static void
  7241. cse_check_loop_start (x, set)
  7242.      rtx x;
  7243.      rtx set;
  7244. {
  7245.   if (cse_check_loop_start_value == 0
  7246.       || GET_CODE (x) == CC0 || GET_CODE (x) == PC)
  7247.     return;
  7248.  
  7249.   if ((GET_CODE (x) == MEM && GET_CODE (cse_check_loop_start_value) == MEM)
  7250.       || reg_overlap_mentioned_p (x, cse_check_loop_start_value))
  7251.     cse_check_loop_start_value = 0;
  7252. }
  7253.  
  7254. /* X is a SET or CLOBBER contained in INSN that was found near the start of
  7255.    a loop that starts with the label at LOOP_START.
  7256.  
  7257.    If X is a SET, we see if its SET_SRC is currently in our hash table.
  7258.    If so, we see if it has a value equal to some register used only in the
  7259.    loop exit code (as marked by jump.c).
  7260.  
  7261.    If those two conditions are true, we search backwards from the start of
  7262.    the loop to see if that same value was loaded into a register that still
  7263.    retains its value at the start of the loop.
  7264.  
  7265.    If so, we insert an insn after the load to copy the destination of that
  7266.    load into the equivalent register and (try to) replace our SET_SRC with that
  7267.    register.
  7268.  
  7269.    In any event, we invalidate whatever this SET or CLOBBER modifies.  */
  7270.  
  7271. static void
  7272. cse_set_around_loop (x, insn, loop_start)
  7273.      rtx x;
  7274.      rtx insn;
  7275.      rtx loop_start;
  7276. {
  7277.   rtx p;
  7278.   struct table_elt *src_elt;
  7279.   static struct write_data init = {0, 0, 0, 0};
  7280.   struct write_data writes_memory;
  7281.  
  7282.   writes_memory = init;
  7283.  
  7284.   /* If this is a SET, see if we can replace SET_SRC, but ignore SETs that
  7285.      are setting PC or CC0 or whose SET_SRC is already a register.  */
  7286.   if (GET_CODE (x) == SET
  7287.       && GET_CODE (SET_DEST (x)) != PC && GET_CODE (SET_DEST (x)) != CC0
  7288.       && GET_CODE (SET_SRC (x)) != REG)
  7289.     {
  7290.       src_elt = lookup (SET_SRC (x),
  7291.             HASH (SET_SRC (x), GET_MODE (SET_DEST (x))),
  7292.             GET_MODE (SET_DEST (x)));
  7293.  
  7294.       if (src_elt)
  7295.     for (src_elt = src_elt->first_same_value; src_elt;
  7296.          src_elt = src_elt->next_same_value)
  7297.       if (GET_CODE (src_elt->exp) == REG && REG_LOOP_TEST_P (src_elt->exp)
  7298.           && COST (src_elt->exp) < COST (SET_SRC (x)))
  7299.         {
  7300.           rtx p, set;
  7301.  
  7302.           /* Look for an insn in front of LOOP_START that sets
  7303.          something in the desired mode to SET_SRC (x) before we hit
  7304.          a label or CALL_INSN.  */
  7305.  
  7306.           for (p = prev_nonnote_insn (loop_start);
  7307.            p && GET_CODE (p) != CALL_INSN
  7308.            && GET_CODE (p) != CODE_LABEL;
  7309.            p = prev_nonnote_insn  (p))
  7310.         if ((set = single_set (p)) != 0
  7311.             && GET_CODE (SET_DEST (set)) == REG
  7312.             && GET_MODE (SET_DEST (set)) == src_elt->mode
  7313.             && rtx_equal_p (SET_SRC (set), SET_SRC (x)))
  7314.           {
  7315.             /* We now have to ensure that nothing between P
  7316.                and LOOP_START modified anything referenced in
  7317.                SET_SRC (x).  We know that nothing within the loop
  7318.                can modify it, or we would have invalidated it in
  7319.                the hash table.  */
  7320.             rtx q;
  7321.  
  7322.             cse_check_loop_start_value = SET_SRC (x);
  7323.             for (q = p; q != loop_start; q = NEXT_INSN (q))
  7324.               if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
  7325.             note_stores (PATTERN (q), cse_check_loop_start);
  7326.  
  7327.             /* If nothing was changed and we can replace our
  7328.                SET_SRC, add an insn after P to copy its destination
  7329.                to what we will be replacing SET_SRC with.  */
  7330.             if (cse_check_loop_start_value
  7331.             && validate_change (insn, &SET_SRC (x),
  7332.                         src_elt->exp, 0))
  7333.               emit_insn_after (gen_move_insn (src_elt->exp,
  7334.                               SET_DEST (set)),
  7335.                        p);
  7336.             break;
  7337.           }
  7338.         }
  7339.     }
  7340.  
  7341.   /* Now invalidate anything modified by X.  */
  7342.   note_mem_written (SET_DEST (x), &writes_memory);
  7343.  
  7344.   if (writes_memory.var)
  7345.     invalidate_memory (&writes_memory);
  7346.  
  7347.   /* See comment on similar code in cse_insn for explanation of these tests. */
  7348.   if (GET_CODE (SET_DEST (x)) == REG || GET_CODE (SET_DEST (x)) == SUBREG
  7349.       || (GET_CODE (SET_DEST (x)) == MEM && ! writes_memory.all
  7350.       && ! cse_rtx_addr_varies_p (SET_DEST (x))))
  7351.     invalidate (SET_DEST (x));
  7352. }
  7353.  
  7354. /* Find the end of INSN's basic block and return its range,
  7355.    the total number of SETs in all the insns of the block, the last insn of the
  7356.    block, and the branch path.
  7357.  
  7358.    The branch path indicates which branches should be followed.  If a non-zero
  7359.    path size is specified, the block should be rescanned and a different set
  7360.    of branches will be taken.  The branch path is only used if
  7361.    FLAG_CSE_FOLLOW_JUMPS or FLAG_CSE_SKIP_BLOCKS is non-zero.
  7362.  
  7363.    DATA is a pointer to a struct cse_basic_block_data, defined below, that is
  7364.    used to describe the block.  It is filled in with the information about
  7365.    the current block.  The incoming structure's branch path, if any, is used
  7366.    to construct the output branch path.  */
  7367.  
  7368. /* Define maximum length of a branch path.  */
  7369.  
  7370. #define PATHLENGTH    10
  7371.  
  7372. struct cse_basic_block_data {
  7373.   /* Lowest CUID value of insns in block.  */
  7374.   int low_cuid;
  7375.   /* Highest CUID value of insns in block.  */
  7376.   int high_cuid;
  7377.   /* Total number of SETs in block.  */
  7378.   int nsets;
  7379.   /* Last insn in the block.  */
  7380.   rtx last;
  7381.   /* Size of current branch path, if any.  */
  7382.   int path_size;
  7383.   /* Current branch path, indicating which branches will be taken.  */
  7384.   struct branch_path {
  7385.     /* The branch insn. */
  7386.     rtx branch;
  7387.     /* Whether it should be taken or not.  AROUND is the same as taken
  7388.        except that it is used when the destination label is not preceded
  7389.        by a BARRIER.  */
  7390.     enum taken {TAKEN, NOT_TAKEN, AROUND} status;
  7391.   } path[PATHLENGTH];
  7392. };
  7393.  
  7394. #ifdef MPW
  7395. #pragma segment CSE03
  7396. #endif
  7397. void
  7398. cse_end_of_basic_block (insn, data, follow_jumps, after_loop, skip_blocks)
  7399.      rtx insn;
  7400.      struct cse_basic_block_data *data;
  7401.      int follow_jumps;
  7402.      int after_loop;
  7403.      int skip_blocks;
  7404. {
  7405.   rtx p = insn, q;
  7406.   int nsets = 0;
  7407.   int low_cuid = INSN_CUID (insn), high_cuid = INSN_CUID (insn);
  7408.   rtx next = GET_RTX_CLASS (GET_CODE (insn)) == 'i' ? insn : next_real_insn (insn);
  7409.   int path_size = data->path_size;
  7410.   int path_entry = 0;
  7411.   int i;
  7412.  
  7413.   /* Update the previous branch path, if any.  If the last branch was
  7414.      previously TAKEN, mark it NOT_TAKEN.  If it was previously NOT_TAKEN,
  7415.      shorten the path by one and look at the previous branch.  We know that
  7416.      at least one branch must have been taken if PATH_SIZE is non-zero.  */
  7417.   while (path_size > 0)
  7418.     {
  7419.       if (data->path[path_size - 1].status != NOT_TAKEN)
  7420.     {
  7421.       data->path[path_size - 1].status = NOT_TAKEN;
  7422.       break;
  7423.     }
  7424.       else
  7425.     path_size--;
  7426.     }
  7427.  
  7428.   /* Scan to end of this basic block.  */
  7429.   while (p && GET_CODE (p) != CODE_LABEL)
  7430.     {
  7431.       /* Don't cse out the end of a loop.  This makes a difference
  7432.      only for the unusual loops that always execute at least once;
  7433.      all other loops have labels there so we will stop in any case.
  7434.      Cse'ing out the end of the loop is dangerous because it
  7435.      might cause an invariant expression inside the loop
  7436.      to be reused after the end of the loop.  This would make it
  7437.      hard to move the expression out of the loop in loop.c,
  7438.      especially if it is one of several equivalent expressions
  7439.      and loop.c would like to eliminate it.
  7440.  
  7441.      If we are running after loop.c has finished, we can ignore
  7442.      the NOTE_INSN_LOOP_END.  */
  7443.  
  7444.       if (! after_loop && GET_CODE (p) == NOTE
  7445.       && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
  7446.     break;
  7447.  
  7448.       /* Don't cse over a call to setjmp; on some machines (eg vax)
  7449.      the regs restored by the longjmp come from
  7450.      a later time than the setjmp.  */
  7451.       if (GET_CODE (p) == NOTE
  7452.       && NOTE_LINE_NUMBER (p) == NOTE_INSN_SETJMP)
  7453.     break;
  7454.  
  7455.       /* A PARALLEL can have lots of SETs in it,
  7456.      especially if it is really an ASM_OPERANDS.  */
  7457.       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
  7458.       && GET_CODE (PATTERN (p)) == PARALLEL)
  7459.     nsets += XVECLEN (PATTERN (p), 0);
  7460.       else if (GET_CODE (p) != NOTE)
  7461.     nsets += 1;
  7462.     
  7463.       /* Ignore insns made by CSE; they cannot affect the boundaries of
  7464.      the basic block.  */
  7465.  
  7466.       if (INSN_UID (p) <= max_uid && INSN_CUID (p) > high_cuid)
  7467.     high_cuid = INSN_CUID (p);
  7468.       if (INSN_UID (p) <= max_uid && INSN_CUID (p) < low_cuid)
  7469.     low_cuid = INSN_CUID (p);
  7470.  
  7471.       /* See if this insn is in our branch path.  If it is and we are to
  7472.      take it, do so.  */
  7473.       if (path_entry < path_size && data->path[path_entry].branch == p)
  7474.     {
  7475.       if (data->path[path_entry].status != NOT_TAKEN)
  7476.         p = JUMP_LABEL (p);
  7477.       
  7478.       /* Point to next entry in path, if any.  */
  7479.       path_entry++;
  7480.     }
  7481.  
  7482.       /* If this is a conditional jump, we can follow it if -fcse-follow-jumps
  7483.      was specified, we haven't reached our maximum path length, there are
  7484.      insns following the target of the jump, this is the only use of the
  7485.      jump label, and the target label is preceded by a BARRIER.
  7486.  
  7487.      Alternatively, we can follow the jump if it branches around a
  7488.      block of code and there are no other branches into the block.
  7489.      In this case invalidate_skipped_block will be called to invalidate any
  7490.      registers set in the block when following the jump.  */
  7491.  
  7492.       else if ((follow_jumps || skip_blocks) && path_size < PATHLENGTH - 1
  7493.            && GET_CODE (p) == JUMP_INSN
  7494.                  && GET_CODE (PATTERN (p)) == SET
  7495.            && GET_CODE (SET_SRC (PATTERN (p))) == IF_THEN_ELSE
  7496.            && LABEL_NUSES (JUMP_LABEL (p)) == 1
  7497.            && NEXT_INSN (JUMP_LABEL (p)) != 0)
  7498.     {
  7499.       for (q = PREV_INSN (JUMP_LABEL (p)); q; q = PREV_INSN (q))
  7500.         if ((GET_CODE (q) != NOTE
  7501.              || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END
  7502.              || NOTE_LINE_NUMBER (q) == NOTE_INSN_SETJMP)
  7503.             && (GET_CODE (q) != CODE_LABEL || LABEL_NUSES (q) != 0))
  7504.           break;
  7505.  
  7506.       /* If we ran into a BARRIER, this code is an extension of the
  7507.          basic block when the branch is taken.  */
  7508.       if (follow_jumps && q != 0 && GET_CODE (q) == BARRIER)
  7509.         {
  7510.           /* Don't allow ourself to keep walking around an
  7511.          always-executed loop.  */
  7512.           if (next_real_insn (q) == next)
  7513.         {
  7514.           p = NEXT_INSN (p);
  7515.           continue;
  7516.         }
  7517.  
  7518.           /* Similarly, don't put a branch in our path more than once.  */
  7519.           for (i = 0; i < path_entry; i++)
  7520.         if (data->path[i].branch == p)
  7521.           break;
  7522.  
  7523.           if (i != path_entry)
  7524.         break;
  7525.  
  7526.           data->path[path_entry].branch = p;
  7527.           data->path[path_entry++].status = TAKEN;
  7528.  
  7529.           /* This branch now ends our path.  It was possible that we
  7530.          didn't see this branch the last time around (when the
  7531.          insn in front of the target was a JUMP_INSN that was
  7532.          turned into a no-op).  */
  7533.           path_size = path_entry;
  7534.  
  7535.           p = JUMP_LABEL (p);
  7536.           /* Mark block so we won't scan it again later.  */
  7537.           PUT_MODE (NEXT_INSN (p), QImode);
  7538.         }
  7539.       /* Detect a branch around a block of code.  */
  7540.       else if (skip_blocks && q != 0 && GET_CODE (q) != CODE_LABEL)
  7541.         {
  7542.           register rtx tmp;
  7543.  
  7544.           if (next_real_insn (q) == next)
  7545.         {
  7546.           p = NEXT_INSN (p);
  7547.           continue;
  7548.         }
  7549.  
  7550.           for (i = 0; i < path_entry; i++)
  7551.         if (data->path[i].branch == p)
  7552.           break;
  7553.  
  7554.           if (i != path_entry)
  7555.         break;
  7556.  
  7557.           /* This is no_labels_between_p (p, q) with an added check for
  7558.          reaching the end of a function (in case Q precedes P).  */
  7559.           for (tmp = NEXT_INSN (p); tmp && tmp != q; tmp = NEXT_INSN (tmp))
  7560.         if (GET_CODE (tmp) == CODE_LABEL)
  7561.           break;
  7562.           
  7563.           if (tmp == q)
  7564.         {
  7565.           data->path[path_entry].branch = p;
  7566.           data->path[path_entry++].status = AROUND;
  7567.  
  7568.           path_size = path_entry;
  7569.  
  7570.           p = JUMP_LABEL (p);
  7571.           /* Mark block so we won't scan it again later.  */
  7572.           PUT_MODE (NEXT_INSN (p), QImode);
  7573.         }
  7574.         }
  7575.     }
  7576.       p = NEXT_INSN (p);
  7577.     }
  7578.  
  7579.   data->low_cuid = low_cuid;
  7580.   data->high_cuid = high_cuid;
  7581.   data->nsets = nsets;
  7582.   data->last = p;
  7583.  
  7584.   /* If all jumps in the path are not taken, set our path length to zero
  7585.      so a rescan won't be done.  */
  7586.   for (i = path_size - 1; i >= 0; i--)
  7587.     if (data->path[i].status != NOT_TAKEN)
  7588.       break;
  7589.  
  7590.   if (i == -1)
  7591.     data->path_size = 0;
  7592.   else
  7593.     data->path_size = path_size;
  7594.  
  7595.   /* End the current branch path.  */
  7596.   data->path[path_size].branch = 0;
  7597. }
  7598.  
  7599. static rtx cse_basic_block ();
  7600.  
  7601. /* Perform cse on the instructions of a function.
  7602.    F is the first instruction.
  7603.    NREGS is one plus the highest pseudo-reg number used in the instruction.
  7604.  
  7605.    AFTER_LOOP is 1 if this is the cse call done after loop optimization
  7606.    (only if -frerun-cse-after-loop).
  7607.  
  7608.    Returns 1 if jump_optimize should be redone due to simplifications
  7609.    in conditional jump instructions.  */
  7610.  
  7611. int
  7612. cse_main (f, nregs, after_loop, file)
  7613.      rtx f;
  7614.      int nregs;
  7615.      int after_loop;
  7616.      FILE *file;
  7617. {
  7618.   struct cse_basic_block_data val;
  7619.   register rtx insn = f;
  7620.   register int i;
  7621.  
  7622.   cse_jumps_altered = 0;
  7623.   constant_pool_entries_cost = 0;
  7624.   val.path_size = 0;
  7625.  
  7626.   init_recog ();
  7627.  
  7628.   max_reg = nregs;
  7629.  
  7630.   all_minus_one = (int *) alloca (nregs * sizeof (int));
  7631.   consec_ints = (int *) alloca (nregs * sizeof (int));
  7632.  
  7633.   for (i = 0; i < nregs; i++)
  7634.     {
  7635.       all_minus_one[i] = -1;
  7636.       consec_ints[i] = i;
  7637.     }
  7638.  
  7639.   reg_next_eqv = (int *) alloca (nregs * sizeof (int));
  7640.   reg_prev_eqv = (int *) alloca (nregs * sizeof (int));
  7641.   reg_qty = (int *) alloca (nregs * sizeof (int));
  7642.   reg_in_table = (int *) alloca (nregs * sizeof (int));
  7643.   reg_tick = (int *) alloca (nregs * sizeof (int));
  7644.  
  7645.   /* Discard all the free elements of the previous function
  7646.      since they are allocated in the temporarily obstack.  */
  7647.   bzero (table, sizeof table);
  7648.   free_element_chain = 0;
  7649.   n_elements_made = 0;
  7650.  
  7651.   /* Find the largest uid.  */
  7652.  
  7653.   max_uid = get_max_uid ();
  7654.   uid_cuid = (int *) alloca ((max_uid + 1) * sizeof (int));
  7655.   bzero (uid_cuid, (max_uid + 1) * sizeof (int));
  7656.  
  7657.   /* Compute the mapping from uids to cuids.
  7658.      CUIDs are numbers assigned to insns, like uids,
  7659.      except that cuids increase monotonically through the code.
  7660.      Don't assign cuids to line-number NOTEs, so that the distance in cuids
  7661.      between two insns is not affected by -g.  */
  7662.  
  7663.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  7664.     {
  7665.       if (GET_CODE (insn) != NOTE
  7666.       || NOTE_LINE_NUMBER (insn) < 0)
  7667.     INSN_CUID (insn) = ++i;
  7668.       else
  7669.     /* Give a line number note the same cuid as preceding insn.  */
  7670.     INSN_CUID (insn) = i;
  7671.     }
  7672.  
  7673.   /* Initialize which registers are clobbered by calls.  */
  7674.  
  7675.   CLEAR_HARD_REG_SET (regs_invalidated_by_call);
  7676.  
  7677.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  7678.     if ((call_used_regs[i]
  7679.      /* Used to check !fixed_regs[i] here, but that isn't safe;
  7680.         fixed regs are still call-clobbered, and sched can get
  7681.         confused if they can "live across calls".
  7682.  
  7683.         The frame pointer is always preserved across calls.  The arg
  7684.         pointer is if it is fixed.  The stack pointer usually is, unless
  7685.         RETURN_POPS_ARGS, in which case an explicit CLOBBER
  7686.         will be present.  If we are generating PIC code, the PIC offset
  7687.         table register is preserved across calls.  */
  7688.  
  7689.      && i != STACK_POINTER_REGNUM
  7690.      && i != FRAME_POINTER_REGNUM
  7691. #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
  7692.      && ! (i == ARG_POINTER_REGNUM && fixed_regs[i])
  7693. #endif
  7694. #ifdef PIC_OFFSET_TABLE_REGNUM
  7695.      && ! (i == PIC_OFFSET_TABLE_REGNUM && flag_pic)
  7696. #endif
  7697.      )
  7698.     || global_regs[i])
  7699.       SET_HARD_REG_BIT (regs_invalidated_by_call, i);
  7700.  
  7701.   /* Loop over basic blocks.
  7702.      Compute the maximum number of qty's needed for each basic block
  7703.      (which is 2 for each SET).  */
  7704.   insn = f;
  7705.   while (insn)
  7706.     {
  7707.       cse_end_of_basic_block (insn, &val, flag_cse_follow_jumps, after_loop,
  7708.                   flag_cse_skip_blocks);
  7709.  
  7710.       /* If this basic block was already processed or has no sets, skip it.  */
  7711.       if (val.nsets == 0 || GET_MODE (insn) == QImode)
  7712.     {
  7713.       PUT_MODE (insn, VOIDmode);
  7714.       insn = (val.last ? NEXT_INSN (val.last) : 0);
  7715.       val.path_size = 0;
  7716.       continue;
  7717.     }
  7718.  
  7719.       cse_basic_block_start = val.low_cuid;
  7720.       cse_basic_block_end = val.high_cuid;
  7721.       max_qty = val.nsets * 2;
  7722.       
  7723.       if (file)
  7724.     fprintf (file, ";; Processing block from %d to %d, %d sets.\n",
  7725.          INSN_UID (insn), val.last ? INSN_UID (val.last) : 0,
  7726.          val.nsets);
  7727.  
  7728.       /* Make MAX_QTY bigger to give us room to optimize
  7729.      past the end of this basic block, if that should prove useful.  */
  7730.       if (max_qty < 500)
  7731.     max_qty = 500;
  7732.  
  7733.       max_qty += max_reg;
  7734.  
  7735.       /* If this basic block is being extended by following certain jumps,
  7736.          (see `cse_end_of_basic_block'), we reprocess the code from the start.
  7737.          Otherwise, we start after this basic block.  */
  7738.       if (val.path_size > 0)
  7739.         cse_basic_block (insn, val.last, val.path, 0);
  7740.       else
  7741.     {
  7742.       int old_cse_jumps_altered = cse_jumps_altered;
  7743.       rtx temp;
  7744.  
  7745.       /* When cse changes a conditional jump to an unconditional
  7746.          jump, we want to reprocess the block, since it will give
  7747.          us a new branch path to investigate.  */
  7748.       cse_jumps_altered = 0;
  7749.       temp = cse_basic_block (insn, val.last, val.path, ! after_loop);
  7750.       if (cse_jumps_altered == 0
  7751.           || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0))
  7752.         insn = temp;
  7753.  
  7754.       cse_jumps_altered |= old_cse_jumps_altered;
  7755.     }
  7756.  
  7757. #ifdef USE_C_ALLOCA
  7758.       alloca (0);
  7759. #endif
  7760.     }
  7761.  
  7762.   /* Tell refers_to_mem_p that qty_const info is not available.  */
  7763.   qty_const = 0;
  7764.  
  7765.   if (max_elements_made < n_elements_made)
  7766.     max_elements_made = n_elements_made;
  7767.  
  7768.   return cse_jumps_altered;
  7769. }
  7770.  
  7771. /* Process a single basic block.  FROM and TO and the limits of the basic
  7772.    block.  NEXT_BRANCH points to the branch path when following jumps or
  7773.    a null path when not following jumps.
  7774.  
  7775.    AROUND_LOOP is non-zero if we are to try to cse around to the start of a
  7776.    loop.  This is true when we are being called for the last time on a
  7777.    block and this CSE pass is before loop.c.  */
  7778.  
  7779. static rtx
  7780. cse_basic_block (from, to, next_branch, around_loop)
  7781.      register rtx from, to;
  7782.      struct branch_path *next_branch;
  7783.      int around_loop;
  7784. {
  7785.   register rtx insn;
  7786.   int to_usage = 0;
  7787.   int in_libcall_block = 0;
  7788.  
  7789.   /* Each of these arrays is undefined before max_reg, so only allocate
  7790.      the space actually needed and adjust the start below.  */
  7791.  
  7792.   qty_first_reg = (int *) alloca ((max_qty - max_reg) * sizeof (int));
  7793.   qty_last_reg = (int *) alloca ((max_qty - max_reg) * sizeof (int));
  7794.   qty_mode= (enum machine_mode *) alloca ((max_qty - max_reg) * sizeof (enum machine_mode));
  7795.   qty_const = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx));
  7796.   qty_const_insn = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx));
  7797.   qty_comparison_code
  7798.     = (enum rtx_code *) alloca ((max_qty - max_reg) * sizeof (enum rtx_code));
  7799.   qty_comparison_qty = (int *) alloca ((max_qty - max_reg) * sizeof (int));
  7800.   qty_comparison_const = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx));
  7801.  
  7802.   qty_first_reg -= max_reg;
  7803.   qty_last_reg -= max_reg;
  7804.   qty_mode -= max_reg;
  7805.   qty_const -= max_reg;
  7806.   qty_const_insn -= max_reg;
  7807.   qty_comparison_code -= max_reg;
  7808.   qty_comparison_qty -= max_reg;
  7809.   qty_comparison_const -= max_reg;
  7810.  
  7811.   new_basic_block ();
  7812.  
  7813.   /* TO might be a label.  If so, protect it from being deleted.  */
  7814.   if (to != 0 && GET_CODE (to) == CODE_LABEL)
  7815.     ++LABEL_NUSES (to);
  7816.  
  7817.   for (insn = from; insn != to; insn = NEXT_INSN (insn))
  7818.     {
  7819.       register enum rtx_code code;
  7820.  
  7821.       /* See if this is a branch that is part of the path.  If so, and it is
  7822.      to be taken, do so.  */
  7823.       if (next_branch->branch == insn)
  7824.     {
  7825.       enum taken status = next_branch++->status;
  7826.       if (status != NOT_TAKEN)
  7827.         {
  7828.           if (status == TAKEN)
  7829.         record_jump_equiv (insn, 1);
  7830.           else
  7831.         invalidate_skipped_block (NEXT_INSN (insn));
  7832.  
  7833.           /* Set the last insn as the jump insn; it doesn't affect cc0.
  7834.          Then follow this branch.  */
  7835. #ifdef HAVE_cc0
  7836.           prev_insn_cc0 = 0;
  7837. #endif
  7838.           prev_insn = insn;
  7839.           insn = JUMP_LABEL (insn);
  7840.           continue;
  7841.         }
  7842.     }
  7843.         
  7844.       code = GET_CODE (insn);
  7845.       if (GET_MODE (insn) == QImode)
  7846.     PUT_MODE (insn, VOIDmode);
  7847.  
  7848.       if (GET_RTX_CLASS (code) == 'i')
  7849.     {
  7850.       /* Process notes first so we have all notes in canonical forms when
  7851.          looking for duplicate operations.  */
  7852.  
  7853.       if (REG_NOTES (insn))
  7854.         REG_NOTES (insn) = cse_process_notes (REG_NOTES (insn), NULL_RTX);
  7855.  
  7856.       /* Track when we are inside in LIBCALL block.  Inside such a block,
  7857.          we do not want to record destinations.  The last insn of a
  7858.          LIBCALL block is not considered to be part of the block, since
  7859.          its destination is the result of the block and hence should be
  7860.          recorded.  */
  7861.  
  7862.       if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
  7863.         in_libcall_block = 1;
  7864.       else if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
  7865.         in_libcall_block = 0;
  7866.  
  7867.       cse_insn (insn, in_libcall_block);
  7868.     }
  7869.  
  7870.       /* If INSN is now an unconditional jump, skip to the end of our
  7871.      basic block by pretending that we just did the last insn in the
  7872.      basic block.  If we are jumping to the end of our block, show
  7873.      that we can have one usage of TO.  */
  7874.  
  7875.       if (simplejump_p (insn))
  7876.     {
  7877.       if (to == 0)
  7878.         return 0;
  7879.  
  7880.       if (JUMP_LABEL (insn) == to)
  7881.         to_usage = 1;
  7882.  
  7883.       /* Maybe TO was deleted because the jump is unconditional.
  7884.          If so, there is nothing left in this basic block.  */
  7885.       /* ??? Perhaps it would be smarter to set TO
  7886.          to whatever follows this insn, 
  7887.          and pretend the basic block had always ended here.  */
  7888.       if (INSN_DELETED_P (to))
  7889.         break;
  7890.  
  7891.       insn = PREV_INSN (to);
  7892.     }
  7893.  
  7894.       /* See if it is ok to keep on going past the label
  7895.      which used to end our basic block.  Remember that we incremented
  7896.      the count of that label, so we decrement it here.  If we made
  7897.      a jump unconditional, TO_USAGE will be one; in that case, we don't
  7898.      want to count the use in that jump.  */
  7899.  
  7900.       if (to != 0 && NEXT_INSN (insn) == to
  7901.       && GET_CODE (to) == CODE_LABEL && --LABEL_NUSES (to) == to_usage)
  7902.     {
  7903.       struct cse_basic_block_data val;
  7904.  
  7905.       insn = NEXT_INSN (to);
  7906.  
  7907.       if (LABEL_NUSES (to) == 0)
  7908.         delete_insn (to);
  7909.  
  7910.       /* Find the end of the following block.  Note that we won't be
  7911.          following branches in this case.  If TO was the last insn
  7912.          in the function, we are done.  Similarly, if we deleted the
  7913.          insn after TO, it must have been because it was preceded by
  7914.          a BARRIER.  In that case, we are done with this block because it
  7915.          has no continuation.  */
  7916.  
  7917.       if (insn == 0 || INSN_DELETED_P (insn))
  7918.         return 0;
  7919.  
  7920.       to_usage = 0;
  7921.       val.path_size = 0;
  7922.       cse_end_of_basic_block (insn, &val, 0, 0, 0);
  7923.  
  7924.       /* If the tables we allocated have enough space left
  7925.          to handle all the SETs in the next basic block,
  7926.          continue through it.  Otherwise, return,
  7927.          and that block will be scanned individually.  */
  7928.       if (val.nsets * 2 + next_qty > max_qty)
  7929.         break;
  7930.  
  7931.       cse_basic_block_start = val.low_cuid;
  7932.       cse_basic_block_end = val.high_cuid;
  7933.       to = val.last;
  7934.  
  7935.       /* Prevent TO from being deleted if it is a label.  */
  7936.       if (to != 0 && GET_CODE (to) == CODE_LABEL)
  7937.         ++LABEL_NUSES (to);
  7938.  
  7939.       /* Back up so we process the first insn in the extension.  */
  7940.       insn = PREV_INSN (insn);
  7941.     }
  7942.     }
  7943.  
  7944.   if (next_qty > max_qty)
  7945.     abort ();
  7946.  
  7947.   /* If we are running before loop.c, we stopped on a NOTE_INSN_LOOP_END, and
  7948.      the previous insn is the only insn that branches to the head of a loop,
  7949.      we can cse into the loop.  Don't do this if we changed the jump
  7950.      structure of a loop unless we aren't going to be following jumps.  */
  7951.  
  7952.   if ((cse_jumps_altered == 0
  7953.        || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0))
  7954.       && around_loop && to != 0
  7955.       && GET_CODE (to) == NOTE && NOTE_LINE_NUMBER (to) == NOTE_INSN_LOOP_END
  7956.       && GET_CODE (PREV_INSN (to)) == JUMP_INSN
  7957.       && JUMP_LABEL (PREV_INSN (to)) != 0
  7958.       && LABEL_NUSES (JUMP_LABEL (PREV_INSN (to))) == 1)
  7959.     cse_around_loop (JUMP_LABEL (PREV_INSN (to)));
  7960.  
  7961.   return to ? NEXT_INSN (to) : 0;
  7962. }
  7963.  
  7964. /* Count the number of times registers are used (not set) in X.
  7965.    COUNTS is an array in which we accumulate the count, INCR is how much
  7966.    we count each register usage.  */
  7967.  
  7968. static void
  7969. count_reg_usage (x, counts, incr)
  7970.      rtx x;
  7971.      int *counts;
  7972.      int incr;
  7973. {
  7974.   enum rtx_code code = GET_CODE (x);
  7975.   char *fmt;
  7976.   int i, j;
  7977.  
  7978.   switch (code)
  7979.     {
  7980.     case REG:
  7981.       counts[REGNO (x)] += incr;
  7982.       return;
  7983.  
  7984.     case PC:
  7985.     case CC0:
  7986.     case CONST:
  7987.     case CONST_INT:
  7988.     case CONST_DOUBLE:
  7989.     case SYMBOL_REF:
  7990.     case LABEL_REF:
  7991.     case CLOBBER:
  7992.       return;
  7993.  
  7994.     case SET:
  7995.       /* Unless we are setting a REG, count everything in SET_DEST.  */
  7996.       if (GET_CODE (SET_DEST (x)) != REG)
  7997.     count_reg_usage (SET_DEST (x), counts, incr);
  7998.       count_reg_usage (SET_SRC (x), counts, incr);
  7999.       return;
  8000.  
  8001.     case INSN:
  8002.     case JUMP_INSN:
  8003.     case CALL_INSN:
  8004.       count_reg_usage (PATTERN (x), counts, incr);
  8005.  
  8006.       /* Things used in a REG_EQUAL note aren't dead since loop may try to
  8007.      use them.  */
  8008.  
  8009.       if (REG_NOTES (x))
  8010.     count_reg_usage (REG_NOTES (x), counts, incr);
  8011.       return;
  8012.  
  8013.     case EXPR_LIST:
  8014.     case INSN_LIST:
  8015.       if (REG_NOTE_KIND (x) == REG_EQUAL)
  8016.     count_reg_usage (XEXP (x, 0), counts, incr);
  8017.       if (XEXP (x, 1))
  8018.     count_reg_usage (XEXP (x, 1), counts, incr);
  8019.       return;
  8020.     }
  8021.  
  8022.   fmt = GET_RTX_FORMAT (code);
  8023.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  8024.     {
  8025.       if (fmt[i] == 'e')
  8026.     count_reg_usage (XEXP (x, i), counts, incr);
  8027.       else if (fmt[i] == 'E')
  8028.     for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  8029.       count_reg_usage (XVECEXP (x, i, j), counts, incr);
  8030.     }
  8031. }
  8032.  
  8033. /* Scan all the insns and delete any that are dead; i.e., they store a register
  8034.    that is never used or they copy a register to itself.
  8035.  
  8036.    This is used to remove insns made obviously dead by cse.  It improves the
  8037.    heuristics in loop since it won't try to move dead invariants out of loops
  8038.    or make givs for dead quantities.  The remaining passes of the compilation
  8039.    are also sped up.  */
  8040.  
  8041. void
  8042. delete_dead_from_cse (insns, nreg)
  8043.      rtx insns;
  8044.      int nreg;
  8045. {
  8046.   int *counts = (int *) alloca (nreg * sizeof (int));
  8047.   rtx insn, prev;
  8048.   rtx tem;
  8049.   int i;
  8050.   int in_libcall = 0;
  8051.  
  8052.   /* First count the number of times each register is used.  */
  8053.   bzero (counts, sizeof (int) * nreg);
  8054.   for (insn = next_real_insn (insns); insn; insn = next_real_insn (insn))
  8055.     count_reg_usage (insn, counts, 1);
  8056.  
  8057.   /* Go from the last insn to the first and delete insns that only set unused
  8058.      registers or copy a register to itself.  As we delete an insn, remove
  8059.      usage counts for registers it uses.  */
  8060.   for (insn = prev_real_insn (get_last_insn ()); insn; insn = prev)
  8061.     {
  8062.       int live_insn = 0;
  8063.  
  8064.       prev = prev_real_insn (insn);
  8065.  
  8066.       /* Don't delete any insns that are part of a libcall block.
  8067.      Flow or loop might get confused if we did that.  Remember
  8068.      that we are scanning backwards.  */
  8069.       if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
  8070.     in_libcall = 1;
  8071.  
  8072.       if (in_libcall)
  8073.     live_insn = 1;
  8074.       else if (GET_CODE (PATTERN (insn)) == SET)
  8075.     {
  8076.       if (GET_CODE (SET_DEST (PATTERN (insn))) == REG
  8077.           && SET_DEST (PATTERN (insn)) == SET_SRC (PATTERN (insn)))
  8078.         ;
  8079.  
  8080. #ifdef HAVE_cc0
  8081.       else if (GET_CODE (SET_DEST (PATTERN (insn))) == CC0
  8082.            && ! side_effects_p (SET_SRC (PATTERN (insn)))
  8083.            && ((tem = next_nonnote_insn (insn)) == 0
  8084.                || GET_RTX_CLASS (GET_CODE (tem)) != 'i'
  8085.                || ! reg_referenced_p (cc0_rtx, PATTERN (tem))))
  8086.         ;
  8087. #endif
  8088.       else if (GET_CODE (SET_DEST (PATTERN (insn))) != REG
  8089.            || REGNO (SET_DEST (PATTERN (insn))) < FIRST_PSEUDO_REGISTER
  8090.            || counts[REGNO (SET_DEST (PATTERN (insn)))] != 0
  8091.            || side_effects_p (SET_SRC (PATTERN (insn))))
  8092.         live_insn = 1;
  8093.     }
  8094.       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  8095.     for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  8096.       {
  8097.         rtx elt = XVECEXP (PATTERN (insn), 0, i);
  8098.  
  8099.         if (GET_CODE (elt) == SET)
  8100.           {
  8101.         if (GET_CODE (SET_DEST (elt)) == REG
  8102.             && SET_DEST (elt) == SET_SRC (elt))
  8103.           ;
  8104.  
  8105. #ifdef HAVE_cc0
  8106.         else if (GET_CODE (SET_DEST (elt)) == CC0
  8107.              && ! side_effects_p (SET_SRC (elt))
  8108.              && ((tem = next_nonnote_insn (insn)) == 0
  8109.                  || GET_RTX_CLASS (GET_CODE (tem)) != 'i'
  8110.                  || ! reg_referenced_p (cc0_rtx, PATTERN (tem))))
  8111.           ;
  8112. #endif
  8113.         else if (GET_CODE (SET_DEST (elt)) != REG
  8114.              || REGNO (SET_DEST (elt)) < FIRST_PSEUDO_REGISTER
  8115.              || counts[REGNO (SET_DEST (elt))] != 0
  8116.              || side_effects_p (SET_SRC (elt)))
  8117.           live_insn = 1;
  8118.           }
  8119.         else if (GET_CODE (elt) != CLOBBER && GET_CODE (elt) != USE)
  8120.           live_insn = 1;
  8121.       }
  8122.       else
  8123.     live_insn = 1;
  8124.  
  8125.       /* If this is a dead insn, delete it and show registers in it aren't
  8126.      being used.  */
  8127.  
  8128.       if (! live_insn)
  8129.     {
  8130.       count_reg_usage (insn, counts, -1);
  8131.       delete_insn (insn);
  8132.     }
  8133.  
  8134.       if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
  8135.     in_libcall = 0;
  8136.     }
  8137. }
  8138.